Goal: I would like to make a dynamic page that allows the visitor to select a month and year from a drop-down menu and have the content (the posts) on the page change according the the values selected.
I’m currently using the following code to display posts from a particular category from a particular month and year.
<?php query_posts("cat=3&monthnum=12&year=2011"); ?> <?php if (have_posts()) : ?>
<ul>
<?php while (have_posts()) : the_post(); ?>
<li>
<?php the_title(); ?>
<?php the_excerpt(); ?>
</li>
<?php endwhile; ?>
</ul><?php endif; ?>
It works well, but I would like to make the page dynamic so that the visitor can select a month and year from a drop-down menu and have the content change according the the values selected. I’ve posted pictures of how it would work here: fivepotato.com/images/ex1.png
and fivepotato.com/images/ex2.png.
To make this work, I know that I will have to make the value of monthnum a variable (which is taken from the dropdown list:
<?php $monthvar = $_POST["month"]; query_posts("cat=3&monthnum=$monthvar&year=2011");?>
I don’t have much experience with Ajax, but I assume that I will need to use it to make the content re-filter once a month is selected from the dropdown menu.
I have found similar inquires on the following site:
askthecssguy.com/2009/03/checkbox_filters_with_jquery_1.html
And I have found a working example similar to what I would like to do at: http://www.babycarers.com/search?ajax=0&searchref=37609&start=0&lat=&lon=&city=&radius=0&spec1=1&spec2=1&spec3=1&spec4=1&spec5=1&spec6=1&spec7=1&inst1=1&inst2=1&inst3=1&inst4=1&inst5=1&inst6=1&inst7=1&minfee=any&maxfee=any&av1=1&keywords=&country=CA&sort=fee&resultsperpage=10
If anyone could help me out with the javascript/Ajax necessary to pull this off I would be greatly appreciative.
Almost 1000 views and not a single comment. Well, I also needed this and decided to make it. I’ve shared the JavaScript and WordPress code below for people in the distant future to use. It looks like a lot, but that’s because I’ve defined some jQuery functions you can use later with
.extend
. All it’s doing is looking for aselect
element (a dropdown) with CSS class.content-filter
.Once found, it uses the dropdown’s id to set a GET variable to the value currently selected, then it redirects to this the same URL and adds these GET variables. For example, if the id of the dropdown was
product_filter
, and this had a value set todate
, then it would set the GET variableproduct_filter=date
. It’s great because it doesn’t care about your Wordpess details – all it cares about is theselect
element.Now the WordPress code. All we really need is to generate the
select
with some kind of id and set the class to.content-filter
. This code asks for a post type like ‘post’ or ‘product’ and makes the select element. It then returns the GET variable for convenience, and if none is set then it defaults to ‘newest’. Notice that the$fields
array sets all the different orderby values you’d like to support. You can always access it anywhere in the template with$_GET['product_filter']
or$_GET['post_filter']
depending on what your type is. This means that only one can exist on any given page, but you want that – otherwise jQuery won’t know which to use. You can extend this code to set a custom id or anything you like later.Now the fun part – putting it together in the content page. All our work pays off with some sweet and short code:
I used the custom post type ‘product’, but if you’re using ‘post’ just replace that. Someone should probably make this into a plugin if they haven’t already 😛