Category archive by month
-
Hello,
I found several posts about this subject with no apparent solution.
I want to have in the sidebar a monthly list of archives from ONLY the selected category (when a category is selected, or when a post that matches that category is being read). At other times there should be no monthly archive.
Is this somehow easily doable? Thanks a bunch if you share your knowledge!
-
I’d really like to second mreddy’s question. It would be greatly needed on my side. TIA
So in your archive.php file there may be something like
<?php if (is_category()) { ?>...
is_category can take a parameter. So, is_category(ID).
What you can maybe do is:
<?php if (is_category(<?php get_the_category(cat_ID) ?>) {<?php get_archives(); ?>} endif; ?>
That would go in your sidebar,Some time ago I came up with a solution to this problem, which was subsequently improved upon by several additional contributors:
https://www.ads-software.com/support/topic/69776?replies=17
That code doesn’t work anymore in 2.13, but today I have got it working again, and here is what you need to do:
1) In wp_get_archives, add:
,'mcat' => false
after the currently last element in the “defaults” array (thanks to youngmicroserf).2) Add this elseif code to the list of potential types:
<br /> elseif ( 'catmonthly' == $type ) {<br /> $arcresults = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS <code></code>year<code>, MONTH(post_date) AS</code>month
, count(ID) as posts FROM $wpdb->posts, $wpdb->post2cat WHERE $wpdb->posts.ID = $wpdb->post2cat.post_id AND $wpdb->post2cat.category_id = ‘$mcat’ AND $wpdb->posts.post_type = ‘post’ AND $wpdb->posts.post_status = ‘publish’ GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC” . $limit);
if ( $arcresults ) {
$afterafter = $after;
foreach ( $arcresults as $arcresult ) {
$url = get_month_link($arcresult->year, $arcresult->month) .”?cat=” .$mcat;
$text = sprintf(__(‘%1$s %2$d’), $wp_locale->get_month($arcresult->month), $arcresult->year);
if ( $show_post_count )
$after = ‘?(‘.$arcresult->posts.’)’ . $afterafter;
echo get_archives_link($url, $text, $format, $before, $after);
}
}
}(Sorry for the long code — pastebin doesn’t seem to be working at the moment)
The query above will work with version 2.13, but pay attention to the $url construction which uses ‘?cat=’. That assumes that the category filter will be the first argument, which will work if your permalinks are set to “Date and name based”. If you change your permalinks to “Default” you will want to replace ?cat= with &cat= here.
3) In sidebar.php, replace the existing Archive section with this:
<br /> <?php /* Whether this is a category archive */ if (is_category()) { ?></p> li><h2>Archives for <?php single_cat_title(''); ?></h2> <ul> <?php $catmonthquery = ("type=catmonthly&mcat=" .($cat)); wp_get_archives($catmonthquery); ?> </ul> /li> <p> <?php </p> <p> } else { ?></p> li><h2>Archives</h2> <ul> <?php wp_get_archives('type=monthly'); ?> </ul> /li> <p> <?php } ?><br />
That all should work correctly — when you are looking at a single category, the archive navigation will only list months with entries for that category and the links will show archives that are filtered for that category.
BONUS: It also occurred to me that when looking at a single post, one might want to modify the archive navigation so that it shoes a separate archive for each category of which that post is a member. To do that, just insert this in the middle of the sidebar code above:
<br /> <?php } elseif (is_single()) {<br /> foreach((get_the_category()) as $cat) { ?></p> li><h2>Archives for <?php echo $cat->cat_name . ' '; ?></h2> <ul> <?php $catmonthquery = ("type=catmonthly&mcat=" .($cat->cat_ID)); wp_get_archives($catmonthquery); ?> </ul> /li> <p>
It goes right after the first
li
.Hope that all is helpful and that people who need it can find this thread.
Perhaps in future releases
(1) a category parameter can be added to wp_get_archives? as in <?php wp_get_archives(‘type=cat=3&limit=15’); ?>
(2) or query_archives as in
<?php query_archives(‘cat=15’); wp_get_archives(); ?>(3) what would be really spiffy if possible is to have wp select the most recent from multiple cats if more than one cat were numbered in the above examples
anyway, easy for me to say…
I’m using 2.2 and got this to look like it loads correctly (I don’t get any errors), but no archive listing actually appears in the sidebar on the category pages.
I get the default “You are currently browsing the archives of…” statement. After that where it normally just says “Archives” it says “Archives for ‘categoryX'” This should be followed by the archive list, but it’s completely blank.
Does anyone know if there were changes to 2.2 that would make this code ‘deprecated’? Is there some other workaround I could use?
I’m guessing I’ve got something in the wrong place, but I’m kind of a noob when it comes to this stuff.
I am using 2.2 as well and seem to have a similar problem as littleoracle.
Has anyone managed to get this working on 2.2?
For the record, the code for the database query is missing a couple of spaces:
SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month, count(ID)
Note the original code has
ASyear
andASmonth
instead ofAS year
andAS month
.I’m still working on the rest (at least, I’m trying to get it altered to do something more specific ;))
Hi there!
I’m trying to get this to work, but I can’t figure it out. I’m having a hard time putting all the pieces together… Could someone who has a working example of this please post it for us not-so-good-at-PHP? ??
Hi, I’m having the same problem as everyone else – I want a monthly archive for each category. I’ve inserted ravirajakumar’s code (thanks!) and it all seems to work – it only shows in the sidebar the months that that category has posts. (TheFixer – if you go here you can see it working)
However, when I click on a month, it goes to the normal monthly archive and shows posts from all the categories. So my question: Is it possible to limit a monthly archive page to a single category?
Hi thefixer, I just got this working, basically You have to add the code from ravirajakumar’s post – steps 1) & 2) to the file called “general-template.php”.
Step 1), you need to add at the end of the “$defaults = array” line 323.
Step 2), you need to add as a new “elseif” statement, under the last one – } elseif ( ( ‘postbypost’ == $type )
Step 3) Just add that code to the page where you want it to appear.
I hope that’s a bit more clear, just lt me know if not and I’ll check the post again.
Cheers
I would like to ask a further question regarding all this. As it is all working I am now pulling in a category template for the archive page. This is the same template I use for the post page as well.
What I would like to do is have a title for the page when you are in the Archive section. So, the code would need to:
– Check to see if you are viewing an archive
– Show a title displaying the Archive month you are viewing.Maybe checking the URL would help?
https://localhost/content/2007/07/?cat=2Can anyone help with this, I can’t find a post about it.
Cheers
Anyone got an idea on this? I’m still stuck. I have tried using the “is_archive” codex type thing, but it generates an error. Could this be because I am using a category template?
My code:
<?php /* Whether this is a category archive */ if (is_archive()) { ?> hello <?php endif; ?>
Any ideas would be greatly appreciated.
Just an update, I’ve figured out how to get the title to display the month of the archive you’re viewing.
Archive for <?php single_month_title(‘ ‘); ?>I still need to figure out how to only show the title when you are actually viewing the archives, even though they are using the same category template as the original post page. Now I have:
<?php if (is_archive()) : ?> <?php /* If this is a category archive */ if (is_archive()) { ?> <h2 class="pagetitle">Archive for the <?php single_month_title(' '); ?></h2> <?php } ?> <?php endif; ?>
This doesn’t generate errors, but the title still shows even when you’re on the post page. How can I get it to work so it only shows when I’m viewing an archive?
Thanks in advance.
No-one able to help here? I just want to know how to change the title for the category template when you are in the archive section. My URL changes from:
This:
https://localhost/content/news/To this:
https://localhost/content/2007/07/?cat=2When you view the archive, can we check against the URL if that would help?
Cheers
Just to follow up again, I am really stuck on this still. I have tried to echo the URL and it comes up as “content/index.php” evertime I go to a page, does this mean I can’t check the URL?
Do permalinks always cause this problem, meaning PHP just reads the URL as index.php?
Please, can anyone point me in the right direction? I’ve spent 4 full days trying to sort this out and I’m getting nowhere.
- The topic ‘Category archive by month’ is closed to new replies.