Button to refresh the page with a value assigned to a variable?
-
Hi,
I am using a combination of WP_Query with ACF to display a subset of data based on a general category (WP_Query loops through the whole category) and a sub-category (based on a field added using ACF for each of the items pulled in by the WP_Query).
Ultimately I’d like one of the category buttons to assign the category to a variable which will be used in my WP_Query and if statement to display matching items only, on the same page. The page starts off displaying all items in all categories (the WP_Query and if statement take their initial arguments from parameters passed to the function called in the page code, I need to overwrite one of those parameter values).
Is it possible, or more rather, can you help out a relative green-horn to have a button refresh the page with a sub-category assigned to a variable so that my existing query and if statement will display the subset of data requested? I’m just not sure how well WordPress is going to take to this very dynamic use of a single page, or if it would just be better to split out the sub-categories to individual WordPress pages (which I know I can do and will work, but it’s not very elegant or efficient).
Here’s my button set (pulled from an ACF Select field):
<div class="category-buttons"> <?php $choices = get_field_choices('website_category'); //var_dump($choices); ?> <ul class="website-categories"> <?php foreach ( $choices as $value => $label ): ?> <li><a href=""><?php echo $label; ?></a></li> <?php endforeach; ?> </ul> <?php //endif; ?> </div>
(get_field_choices() is a custom function a clever person over on stackexchange coded to get the choices from the ACF post metadata).
Here’s my main query loop:
<?php if ($sub_category) { ?> <div class="portfolio"> <?php $args = array( 'post_type' => 'portfolio', 'category_name' => $category, 'posts_per_page' => -1, 'orderby' => 'date', 'order' => 'ASC', ); $portfolio_query = new WP_Query( $args ); $i = 1; while($portfolio_query->have_posts()): $portfolio_query->the_post(); if ((get_field('website_category') == $sub_category) && ($i <= $limit)) { $i++; // html to display the results here
I’m thinking there might be some built-in WordPress function I don’t know about, or some simple javascript, just need pointing in the right direction.
- The topic ‘Button to refresh the page with a value assigned to a variable?’ is closed to new replies.