Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've created a custom post type called news. On the page that shows all of my news posts i need to have a menu on the left side (sidebar) which shows months, i.e. October 2011, September 2011 and so on. When I click on one of these months i want it to be able to show the posts from that month as sub items in the menu as well as filter the posts that are shown on the page to only show posts from that month.

It would also need to auto update the months, i.e. when we go in to November, November 2011 will be added to the menu automatically.

I plan on using a plugin called Jquery Accordion Menu but that's not necessary if the menu displays the sub items. The plugin is just a widget that takes a menu created in wordpress and gives it an jquery accordion style to expand menu items and show sub items.

Any ideas how this can be achieved.

Thanks

share|improve this question
    
possible duplicate of Auto add pages to category menu –  kaiser Oct 11 '11 at 16:19
    
I realise it's a similar question but i felt it was different enough to warrant a separate question. That was about adding menu items as subs to categories. This is about showing and filtering posts based on their date. –  Corbula Oct 11 '11 at 16:38
    
That's why it states "possible". The difference in detail is just the query arg, so I'd say it is a dublicate as the task is the same. Try to get around one thing first and then ask the second one. Also please try to follow the hints I gave you in the comments to my A on your other Q regarding "How to ask and what to provide". –  kaiser Oct 11 '11 at 16:53
add comment

1 Answer

up vote 0 down vote accepted

You seem to know WP so I'll just give you the outline (This recent post from my blog includes the details anyway). The best way to do this would be to use query variables. From the comments, I gather this is what kaiser is saying.

The link (for October 2011, say) could point example.com/news/2011/10

You first add a' generate_rewrite_rules' filter that adds a rule, so WordPress reads this as example.com/news/?year=2011&monthnum=10,

Another filter(s), e.g. post_where, alters the query to restrict the returned posts to 'news' items in that year and month. Note, this method will not work if your news page uses query_posts() or WP_Query() inside its template page to get the relevant posts. If you're using that method, you'll need some if statements inside the template to determine if year/month is set and then alter the arguments being fed into either of those functions.

As for auto-generating the menu - you can use the php Date() function to retrieve the current month and year, and then a while loop to go through a display the last 6 months, along with the relevant link. For instance, inside a <ul> tag you could put the php code

$i=0
while ($i<6){
    $yearInt = intva(date("Y", strtotime("-".$i." months")));
    $monInt = intva(date("n",  strtotime("-".$i." months")));
    $monStr = date("F",  strtotime("-".$i." months"));
    $yearStr = date("Y",  strtotime("-".$i." months"));

    echo "<li><a href='www.example.com/news/".$yearInt."/".$monInt."'>".$monStr." ".$yearStr"</a></li>";

    $i = $i +1;
}

Hope this helps :D

If you are using query_posts(), then presumably you have some argument that calls up your 'news' posts. What you'll want to do is add something like the following, which checks if a year and month have also been selected. You'll want to convert the month and year into integers yearInt, monthInt (see above), and use these to adapt the argument in the query_posts() function

global $wp_query;
if (isset( $wp_query->query_vars['monthnum'] ) & isset( $wp_query->query_vars['year'] )){
    //add additional arguments to query_var(): '&year=yearInt&monthnum=monthInt'
}else{
    //Normal query_vars() (i.e. what you had before)

}
share|improve this answer
    
Thanks for this answer but i'm stuck trying to follow your blog. Although i know some of WP i don't know a lot. I'm following your blog and after the first sections 'Query Variable' and 'Calling up a template' i tried the page but i'm all i'm getting is a link to the current page i.e. /news and all of my news posts. I was using query_posts() in the template to display the posts but i've removed that now. Can you please offer any assistance. Thanks. pastebin.com/P6YuSGue –  Corbula Oct 12 '11 at 13:13
    
Yes, it should return all posts, most recent first (much like a standard index.php). If you read the next section on 'Custom queries of the database' that will tell you how to retrieve your news posts. But, it will only work if you have used the custom value/key in the 'post_meta' table to distinguish news items. But you've used post_type, so that section would need altering. I've edited my answer which may work with query_vars() –  Stephen Harris Oct 12 '11 at 14:36
    
Thanks, but to be honest a lot of this went over my head. I have however found this plugin which does pretty much what i want. However the links it has to the months i.e. /2011/10 for October just gives a page not found. wordpress.org/extend/plugins/smart-archives-reloaded –  Corbula Oct 13 '11 at 10:58
    
I got this working using a plugin called Collapsing Archives. –  Corbula Nov 24 '11 at 16:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.