I have a conditional statement to show certain images to choose from (radio buttons) based on the category chosen, however when the category is chosen, the radio buttons don’t show? What am I doing wrong?
<fieldset class="category">
<label for="cat">Post Category:</label>
<?php wp_dropdown_categories( 'tab_index=3&taxonomy=category&hide_empty=0' ); ?>
</fieldset>
<?php $categoryId = $_POST['cat']; ?>
<?php if ($categoryId == '3') { ?>
<fieldset class="cat1">
<label for="cat1">Please choose from 1 of the images below for your Post: </label><br>
<input type="radio" name="hate" value="<?php bloginfo('url'); ?>/wp-content/uploads/2012/08/hate1.jpg" id="hate1" /> <img src="<?php bloginfo('url'); ?>/wp-content/uploads/2012/08/hate1.jpg" alt="" height="167" /><br />
<input type="radio" name="hate" value="<?php bloginfo('url'); ?>/wp-content/uploads/2012/08/hate2.jpg" id="hate2" />
<img src="<?php bloginfo('url'); ?>/wp-content/uploads/2012/08/hate2.jpg" alt="" height="167" />
</fieldset>
<?php } elseif ($categoryId == '4') { ?>
<fieldset class="cat2">
<label for="cat2">Please choose from 1 of the images below for your Post: </label><br>
<input type="radio" name="love" value="<?php bloginfo('url'); ?>/wp-content/uploads/2012/08/love1.jpg" id="love1" /> <img src="<?php bloginfo('url'); ?>/wp-content/uploads/2012/08/love1.jpg" alt="" height="167" /><br />
<input type="radio" name="love" value="<?php bloginfo('url'); ?>/wp-content/uploads/2012/08/love2.jpg" id="love2" /> <img src="<?php bloginfo('url'); ?>/wp-content/uploads/2012/08/love2.jpg" alt="" height="167" />
</fieldset>
<?php } else echo 'Damnit!'; ?>
To do what you are trying to do, you need to use
add_query_arg()
andget_query_var()
, and then use your query arg/value to modify the query.I’ll give you an example of how I create similar “filters” in one custom Theme; you’ll need to modify it slightly to conform to your form-field markup rather than using merely an anchor, like I do.
First, you need to add your query arg, like so:
This link essentially reloads the current page, with
cat_filter=some-cat
appended to the URL query string.Second, you need to determine if your query arg has been set:
(Note: you’ll want to do some sanitization here, for data security.)
If you were working with a custom query via
WP_Query
, you could do this check directly in the template, and then add the result to your custom query arguments array. But, if you’re working with the main loop query, you’ll want to wrap this inside a callback to filterpre_get_posts
:If you want to clear the filter, you can use
remove_query_arg()
.