Kwebble : blog

  • Archives for a category WordPress plugin

    Gepubliceerd op 15 augustus 2007 in Programming. 93 reacties

    For the new version of this website I wanted to show a list of monthly archives, limited to posts of a specific category. The software used to run this website is WordPress but the wp_get_archives() function, to get a list of archives, does not have the ability to filter on category. So I created the ‘Archives for a category’ plugin that enhances this function to show archive links for a specific category.

    Installation

    1. Download the plugin zip file and unzip the file in a temporary directory.
    2. Put the kwebble_archives_by_cat.php file in your WordPress plugin directory:
      <your wordpress dir>/wp-content/plugins
    3. Activate the plugin ‘Archives for a category’ on the Plugin Management page of the WordPress administration panel.
    4. Optionally disable the canonical URLs from the menu Settings | Kwebble.
      A WordPress feature added in version 2.3, called canonical URLs, redirects browsers on certain URLs. This also happens with the URLs for the archives with a cat parameter. This causes the archive pages to contain posts which do not belong to the selected period.

      To solve this problem the plugin can disable canonical URLs. This uses the technique used in the Disable Canonical URL Redirection plugin Mark Jaquith made. So if you are already using that plugin you don’t need to change the setting for this plugin.
      To disable canonical URLs go to the administration section of your blog, choose Settings and the Kwebble settings. On that page you will find a checkbox to disable canonical URLs.

    Usage

    After installing and activating the plugin the wp_get_archives() function accepts a ‘cat’ parameter to specify the categories of posts to show in the list of archives. The value of the ‘cat’ parameter must be a list of one or more category ID’s, separated by comma’s.

    If you specify the value of a category ID the posts from that category will be used to create the list of archives. If you place a minus sign ‘-’ in front of an ID the posts from that category will be excluded.

    Depending on the number of categories, your use of them and selection of categories to include in the archive list it may be easier to specify all categories to include or just those to exclude.

    You need to make sure the template used to show each archive displays posts from the selected category. I’m using category specific templates on this site, like category-id where id is the ID of the category to display. You can use other templates in the template hierarchy, but make sure the template shows items of the categories you specify.

    At some WordPress version the categories ID’s are no longer visible on the administration pages. You can find the ID of a category by opening the page to edit the category and inspect the URL of that page. The value after cat_ID= is the ID of the category.

    Examples

    Show the default monthly list of archives for category 1:

    <?php wp_get_archives('cat=1'); ?>
    

    The same list, but with posts from categories 1 and 3:

    <?php wp_get_archives('cat=1,3'); ?>
    

    Use posts from all categories except category 2:

    <?php wp_get_archives('cat=-2'); ?>
    

    Use posts from all categories except categories 2 and 8:

    <?php wp_get_archives('cat=-2,-8'); ?>
    

    Create a list of archives for category 1 as a dropdown box:

    <?php wp_get_archives('format=option&cat=1'); ?>
    

    Limitations

    This plugin does not work for weekly archives. The list with archive links is correct, but the links themselves do not include the category. So when used, WordPress will not filter the resulting page on the category. The technical reason is that WordPress does not apply filters when the links for weekly archives are generated, so the plugin can’t change them. Perhaps in a next version of WordPress…

    This plug-in was developed and tested to work correctly with WordPress version 2.7.1, but it probably works with earlier versions back to 2.2.1.

    Older versions

    Version 1.4a

    27-03-2009 Download

    • Corrected post count when posts belong to multiple categories.
    • SQL queries now respect the configured SQL table prefix.
    • Templates with multiple calls to wp_get_archives(), with and without ‘cat’ parameter, generate correct URL’s.

    Version 1.4

    22-02-2009 – Added option to exclude categories. Download.

    Version 1.3

    6-01-2008 – Added support for multiple categories. Download.

    Version 1.2

    23-11-2007 – Added support for WordPress 2.3.1. Download.

    Version 1.0

    15-08-2007 – Initial version, works with WordPress 2.2.1. Download.

    Copyright

    Copyright 2007, 2008, 2009 Rob Schlüter. All rights reserved.

    Licensing terms

    • You may use, change and redistribute this software provided the copyright notice above is included.
    • This software is provided without warranty, you use it at your own risk.

    83 Reacties

    1. Avatar

      Queenvictoria 4-10-2007 (22:08)

      thanks kwebble for this . here is the update to make it work with the v2.3 version of wordpress .

      function kwebble_getarchives_where_for_category($where, $args){
      global $kwebble_getarchives_data;

      if (isset($args['cat'])){
      // Preserve the category for later use.
      $kwebble_getarchives_data['cat'] = $args['cat'];

      if ( get_bloginfo('version') > 2.3 )
      $where .= ' AND post2cat.category_id=' . $args['cat'];
      else
      $where .= " AND taxonomy.taxonomy = 'category' AND taxonomy.term_id = " . $args['cat'];
      }
      return $where;
      }

      and later


      function kwebble_getarchives_join_for_category($join, $args){
      global $wpdb;

      if (isset($args['cat'])){
      if ( get_bloginfo('version') > 2.3 )
      $join .= ' JOIN ' . $wpdb->post2cat . ' post2cat ON post2cat.post_id=ID';
      else
      $join = " LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = taxonomy.term_taxonomy_id) ";

      }
      return $join;
      }

    2. Avatar

      Queenvictoria 4-10-2007 (22:14)

      sorry that should be less than not greater than 2.3

      function kwebble_getarchives_where_for_category($where, $args){
      global $kwebble_getarchives_data;

      if (isset($args['cat'])){
      // Preserve the category for later use.
      $kwebble_getarchives_data['cat'] = $args['cat'];

      if ( get_bloginfo('version')

      and later


      function kwebble_getarchives_join_for_category($join, $args){
      global $wpdb;

      if (isset($args['cat'])){
      if ( get_bloginfo('version') post2cat . ' post2cat ON post2cat.post_id=ID';
      else
      $join = " LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = taxonomy.term_taxonomy_id) ";

      }
      return $join;
      }

    3. Avatar

      Rob Schlüter 5-10-2007 (20:00)

      Thanks, I didn’t realize the plugin needed an upgrade for the new Wordpress version.

      I’ll test this when I upgrade my site to the 2.3 version and if everything is ok I will post a new version of the plugin.

    4. Avatar

      Mike 31-10-2007 (20:22)

      Just installed the plugin myself (within WP v2.3.1) and it needed queenvictoria’s modification.

      However, if you are going to try it yourself: just copy & paste the code from queenvictoria’s FIRST post (from “4 october 2007, 22:08) and then modify the ‘greater than’ symbol to a ‘less than’ by hand in BOTH instances.

      It looks as if the code got messed up when submitting the second post (from “4 october 2007, 22:14″)

    5. Avatar

      Rob Schlüter 1-11-2007 (9:28)

      Thanks for the note.

      In the coming days I’m upgrading my development version of the site to WordPress 2.3.1. Then, if everything works ok, I’ll upgrade my site and make a new version of this plugin available.

    6. Avatar

      jay 20-11-2007 (16:11)

      If you’re going to use this with WordPress 2.3, you’ll have to use the plugin which disables canonical redirects. The new version of Wordpress doesn’t correctly process the URLs to show archives only in a given cat. (Specifically URLs like /2007/10/?cat=1 won’t work unless canonical URLs are disabled.)

      Here’s the plugin…
      http://txfx.net/files/wordpress/disable-canonical-redirects.phps

    7. Avatar

      Rob Schlüter 21-11-2007 (21:58)

      At the moment I’m testing a new version of the plugin and also noticed that the URL’s generated by the plugin are redirected incorrectly creating archive pagess with incorrect posts.

      To solve this I’m adding the possibility to disable canonical URLs from the administration menu.

    8. Avatar

      Rob Schlüter 23-11-2007 (23:30)

      A new version (1.2) with WordPress 2.3 support is now available.

    9. Avatar

      stephen 29-12-2007 (10:12)

      This is really great, but I was wondering if there was a way to show multiple categories.

    10. Avatar

      Rob Schlüter 30-12-2007 (23:57)

      Using multiple categories is not possible at the moment. I’ll have a look to see if I can add support for more than 1 category.

    11. Avatar

      steve 1-01-2008 (23:24)

      That would be great!

    12. Avatar

      Rob Schlüter 6-01-2008 (14:29)

      Just updated the plugin to version 1.3, which adds support for multiple categories.

    13. Avatar

      jack 22-01-2008 (2:32)

      exactly what I needed, thanks!

    14. Avatar

      Tony 23-01-2008 (4:34)

      Hi, this was exactly what I was looking for! And using it along with the category-id.php works very well! Thank you so much!

    15. Avatar

      cj 25-02-2008 (23:33)

      Hi, love the plug-in, it works great. One question though, is there a way to make the monthly category archives use the archive template instead of the category template? Thanks

    16. Avatar

      Rob Schlüter 26-02-2008 (13:19)

      cj, perhaps you could rename the category template and create a new one that checks if a category or archive page is requested, and include() the appropriate template.

      I think you’ll have check for a part in the URL that identifies the requested page type, for example a date pattern, because the is_archive() is also true for category pages, according to the Codex .

    17. Avatar

      slambert 11-03-2008 (6:46)

      this is great. Curious if it’s possible to show the number of posts next to the month and year. for example August 2007 (6) If there were 6 posts from that month.

    18. Avatar

      Rob Schlüter 14-03-2008 (21:37)

      @slambert: does the parameter show_post_count you can use with wp_get_archives do what you need?

    19. Avatar

      slambert 16-03-2008 (23:55)

      @rob Schülter: yes and no. That’s the functionality, but I still want to filter it by category. Make sense?

    20. Avatar

      Zef 13-04-2008 (0:41)

      Just posting to say thank you SO much for creating this plugin. I’ve had a day of searching and you have saved my website!

    21. Avatar

      Tom 28-04-2008 (20:07)

      I just noticed that this plugin doesn’t work in Wordpress 2.5. Any plans to update it? Thanks for your work so far — it’s a very helpful plugin.

    22. Avatar

      Rob Schlüter 28-04-2008 (22:01)

      I will investigate what’s the reason 2.5 breaks the plugin.

    23. Avatar

      tom 20-05-2008 (14:37)

      @rob: I was wrong. It IS in fact working in Wordpress 2.5. Sorry for the confusion.

    24. Avatar

      Chi 21-05-2008 (17:15)

      Hi.. Nice plugin.. Unfortunately, it isn’t retreiving children category posts.. Pls help!

    25. Avatar

      Rob Schlüter 22-05-2008 (8:12)

      It need some time to find out how to generate a URL that includes all child categories.

      If the category structure of your site isn’t that complex and doesn’t change often then, as a temporary solution, you could add the child categories to the cat parameter, separated by comma’s:

      <?php wp_get_archives('cat=1,3,7,8,19'); ?>

      This will include posts from all those categories in the archive.

    26. Avatar

      Jeff 3-07-2008 (13:13)

      Just wanted to say thank you. Really appreciate the work :)

    27. Avatar

      Nicole 16-07-2008 (0:42)

      Hi there,
      great plugin and easy to use!
      Just wondering, I want to Archive via Year, not month.. can this plugin do that?

    28. Avatar

      Rob Schlüter 16-07-2008 (8:17)

      wp_get_archives can generate yearly archives by using the type parameter, which is part of WordPress itself. Use something like this to combine it with a category selection:

      <?php wp_get_archives('type=yearly&cat=1'); ?>

    29. Avatar

      ann 15-08-2008 (20:08)

      thanks! this saved me tons of headache!

    30. Avatar

      Steve Meisner 2-09-2008 (20:04)

      Thanks, it works great in 2.6.1.

    31. Avatar

      Paul 8-10-2008 (11:01)

      with this plugin will the function accept negative value, that is can this be used to EXCLUDE certain categories from being listed, that is filter out ?

    32. Avatar

      Rob Schlüter 8-10-2008 (12:39)

      No, you can’t exclude categories by using negative values.
      It would require some changes to the way the cat parameter is placed in the SQL to select matching posts.

    33. Avatar

      Paul 8-10-2008 (12:48)

      ok, i can work around it – im my case im trying to use it to publish a news (facts) index section, actually a category, and keep it separate to a blog (opinion) section even though they run off 99% similar standard blog templates.

    34. Avatar

      Carl-Johan 20-12-2008 (0:19)

      Great plugin! This should really become a basic function in wordpress. Maybe you could submit it to them.

    35. Avatar

      Rob Schlüter 20-12-2008 (14:08)

      @Carl-Johan in the current situation this plugin conflicts with the canonical URLs feature. That should be resolved to be included, I expect.
      But surely I agree :-) , it would be a useful addition.

    36. Avatar

      anders 21-01-2009 (12:36)

      Hi!

      Is it possible to exclude categories with this plugin this also?

      f.ex:
      wp_get_archives(’type=monthly&cat=-7′);

      If not it would be great if you added this in a later edition! ;D

    37. Avatar

      Rob Schlüter 21-01-2009 (13:36)

      @anders: no, excluding doesn’t work. I’ll have a look to see if it’s not too difficult to add since you are the second person asking for it.

    38. Avatar

      anders 23-01-2009 (9:02)

      thanks! ;D

      and one more thing!
      Is it a way to display a category (by ID) and all childrens of that category, without typing their ID in?

    39. Avatar

      Russ 2-02-2009 (19:45)

      Great plugin! It did EXACTLY what I needed it to. Super-easy and brilliant!

    40. Avatar

      luke 3-02-2009 (20:00)

      thanks!
      exactly what i need…..

    41. Avatar

      Susan 9-02-2009 (19:07)

      Amazing — I searched for something I had expected WP to handle but did not, ie,:

      and you immediately solve the problem with #1 hit.
      Thank you!

    42. Avatar

      Susan 9-02-2009 (19:09)

      search term didn’t show up. It was

    43. Avatar

      Susan 9-02-2009 (19:10)

      one more try
      “”

    44. Avatar

      Jonathan 16-02-2009 (2:42)

      Would also love it if this would allow for category exclusion :) .

    45. Avatar

      Sarah 17-02-2009 (11:33)

      I wish this was 2.7 compatible.
      Is there any chance you’ll update it?
      Please?

    46. Avatar

      Rob Schlüter 17-02-2009 (14:31)

      @sarah: I haven’t heard about problems with 2.7 and since I don’t use that version myself, could you let me know what problems you encounter?

    47. Avatar

      damien 20-02-2009 (13:04)

      @Rob Schlüter
      and How to exclude categories?
      and can I create a list of archives, with posts from categories 1 AND 3 (! its not a 1 OR 3)?

    48. Avatar

      Rob Schlüter 20-02-2009 (23:03)

      Sorry Damien, at the moment only posts from the selected categories, no AND/OR/EXCLUDE.

    49. Avatar

      Rob Schlüter 22-02-2009 (22:55)

      Version 1.4 is now available. Now you can exclude posts from categories by putting a minus sign (-) in front of a category ID.

      It’s a fresh version that’s only been tested on a development environment, but it looks OK. Enjoy.

    50. Avatar

      damien 23-02-2009 (12:59)

      How to use your plugin with the tags?
      Thank you for updating!

    51. Avatar

      Rob Schlüter 23-02-2009 (13:44)

      @damien: see ‘Examples’ on this page for the syntax you have to use.

    52. Avatar

      peach 3-03-2009 (23:38)

      fyi – works great in wp 2.7.1. thanks!

    53. Avatar

      Vlad 17-03-2009 (8:30)

      Hello Rob,

      Thanks for developing this plugin.

      I think I have found a bug – if I have a post that belongs to two categories, it will be displayed twice in the archives when I use the &cat parameter. The only way to avoid this is to make the cat parameter specify ONE of the categories it belongs to.

      For example, if my post is a member of categories 3 and 5 and I do &cat=-1, it will be listed twice, but if I do &cat=-1,3 then the post will be listed once.

      I think this just requires a small change to the plugin’s WHERE or JOIN query, but I cannot figure it out myself as I am unfamiliar with WordPress’ schema.

      I am using WordPress 2.7.1.

    54. Avatar

      wp 23-03-2009 (4:06)

      this plugin is a lifesaver. thanks!

    55. Avatar

      danjothebanjo 25-03-2009 (17:58)

      Hi There – your plugin is the only thing out there that does what I needed thanks! Just one thing i saw above that one of your users also wondered how to force the request to use the archive template rather than the category template. As soon as I pass the cat parameter to the function it starts using the category template? Any ideas?
      Thanks again.

    56. Avatar

      Rob Schlüter 25-03-2009 (22:10)

      @vlad: correct, in the described situation the post count is wrong. I’m figuring out a solution

      @danjothebanje: true, according to the template hierarchy when a category is present in the request the category template is preferred over the archive template. Perhaps you can use is_archive() in the category template to test if an archive is requested or not and include the appropriate template.

    57. Avatar

      danjothebanjo 26-03-2009 (12:25)

      thanks Rob, I actually used is_date() in a conditional statement in my category template to get the template looking right – thanks again

    58. Avatar

      Rob Schlüter 27-03-2009 (13:58)

      Version 1.4a is available for download. Changes:
      - Corrected post count when posts belong to multiple categories.
      - SQL queries now respect the configured SQL table prefix.
      - Templates with multiple calls to wp_get_archives(), with and without ‘cat’ parameter, generate correct URL’s.

      enjoy

    59. Avatar

      Becky 27-03-2009 (23:56)

      It’s not working for me. I used and nothing is showing up. What am i missing?

    60. Avatar

      Rob Schlüter 28-03-2009 (12:25)

      Becky, could you give me some more information? What is not showing, the list of archives or the complete page with archived posts?

      And how does the call to wp_get_archives() look like? Does it include the ‘cat’ parameter to select categories?

    61. Avatar

      bill 3-04-2009 (23:23)

      Rob- Thanks for your work on this plugin.

      If I understand correctly, in order to get the sidebar archive widget to only show a particular category, I need to edit the widget.php file as follows:

      becomes

      There are 2 instances of the wp_get_archives() in widgets.php. Do I need to edit both? Am I doing the edits in correct file? Thanks.

    62. Avatar

      Rob Schlüter 5-04-2009 (19:06)

      @bill: I haven’t used widgets and don’t really know much about them. widgets.php appears to me as a collection of widgets that come with WordPress. Adding the ‘cat’ parameter probably changes the ‘Archives’ widget and I expect it to work fine. But since you’re editing a core file be careful that updating WordPress doesn’t overwrite your changes!

    63. Avatar

      Selsobesy 8-04-2009 (10:06)

      mm. thank you..

    64. Avatar

      Dorothea Schäfer 9-04-2009 (0:13)

      Great!
      Theank you very much.

    65. Avatar

      Jukka 18-04-2009 (23:10)

      Hi!

      I have a small problem with the plugin. It works like a charm with current month, but not for earlier months.

      I have two categories. If there aren’t any posts in category 1 for say January, the month won’t show on the archive list, so that’s fine. But when I call posts from catgory 1 and there are some, the plugin also shows posts from category 2 for that month.

      Do you have any idea why this is happening?

    66. Avatar

      Rob Schlüter 19-04-2009 (16:53)

      @jukka: it’s a bug. I’ve resolved the problem and updated the plugin. The new version (1.4b) is available for download.

      In this version the support for multiple calls to wp_get_archives(), with and without ‘cat’ parameter, is removed. This feature was the cause of the bug.

    67. Avatar

      Jukka 20-04-2009 (7:49)

      Thank man! Works great now.

    68. Avatar

      Karl 21-04-2009 (13:28)

      Hi there,

      Thanks for the plugin, just what I needed! Agree with posters above, would be great if this was simply built into WP core, pretty logical call.

      Another related issue I’ve never been able to solve, is giving the currently viewed archive a current-class – is this anything that could be built into your plugin? Then one can use CSS to list which month is being viewed.

    69. Avatar

      Vince Brown 21-04-2009 (22:16)

      Thanks mate

    70. Avatar

      Rob Schlüter 22-04-2009 (11:55)

      @karl, marking the currently viewed archive looked like a nice challenge so I did some testing and came up with a basic solution.

      But the plugin parses the generated HTML for the archives and that’s not something I like. I want to see if I can come up with a better solution and publish it as part of this plugin or as a separate one.

    71. Avatar

      Saeid Zebardast 24-04-2009 (15:45)

      Thank you :)
      Its useful.

    72. Avatar

      Stereo Interactive 25-04-2009 (18:47)

      Thank you Rob, it is so nice of you to share this with us all.

    73. Avatar

      Karl 27-04-2009 (9:24)

      @Rob,

      Thanks for looking into the highlighting issue – would be interesting to hear what you come up with!

    74. Avatar

      Eric Curtis 7-05-2009 (2:21)

      Just wanted to say thank you for this plug-in. It was just what I needed for my last project.

      FYI, I found a conflict with this and WP-Security Scan. When both were active your plug-ins functionality was broken.

      Cheers.

    75. Avatar

      Cisco 15-05-2009 (10:43)

      Hello, thanks for your plugin,
      can you add the parameter “child_of” ? Because when I type the global category, there’s no list.

    76. Avatar

      june 18-05-2009 (3:00)

      I dropped this my plugins directly and its not showing up in my admin interface. I’ve installed other plugins without a problem. Using 2.7.1

    77. Avatar

      Rob Schlüter 18-05-2009 (20:05)

      Thanks for the responses. Perhaps a small forum would be easier for communicating issues and new features then these comments…

    78. Avatar

      Brad 21-05-2009 (22:16)

      This seems to be the only solution for excluding cats from get archives, i cant get it to work though sadly,

      The archive list goes blank when i add cat=-2 but works when i remove it

      Im using 2.7.1

    79. Avatar

      pirkka 22-05-2009 (16:15)

      I have the same problem.

    80. Avatar

      Scott 28-05-2009 (11:31)

      Just wanted to say thanks for the plugin, works great for me on 2.7.1.

    81. Avatar

      Ash Haque 6-06-2009 (8:14)

      Thank you!
      This was exactly what I was looking for, works great

    82. Avatar

      Matt 11-06-2009 (14:22)

      I’m having trouble as I have this: domain…./2009/05/?cat=4 when I click it though, it fwds to the url of the category: domain…category/the-category/ and it displays all the posts for all the months, rather than the posts for just may. Any ideas?

    83. Avatar

      Rob Schlüter 12-06-2009 (13:01)

      @matt: perhaps it helps to disable canonical URL redirect. You can do this on the Settings > Kwebble page.

      Note that this influences other URL’s handling also, see the linked page for more info.

    Trackbacks

    1. WordPress 2.3 compatiblity Plugins | Dreamer's Blog
    2. WordPress Plugins Database » Plugin Details » Archives for a category
    3. 2pt3 › archive » wordpress tips
    4. AWSOM.org = Artist Website Setup Options Markup » Blog Archive » AWSOM Archive 1.4.0 released
    5. Archives for a category WordPress plugin
    6. DIY Podcasting on Wordpress | How-tos | ministrypool.com
    7. Removing Categories From A Wordpress Feed | /timelliott
    8. Plugins para Wordpress « La mala memoria
    9. Sugar Blog » Westgate Dental gets a facelift
    10. Archives for a category WordPress plugin (Kwebble blog) « wp-popular.com

    Laat een reactie achter

  • Rubrieken

    • Artikelen
    • Foto's
    • Links
    • Overheid
    • Programming
  • Recente Berichten

    • Federal IT Dashboard
    • NoSQL
    • Smiley guppy
    • Kwebble 4.0
    • Rotterdams havenbedrijf laat natuur aanleggen?
  • Abonneren

    • Atom feed Artikelen
    • Atom feed Reacties
  • Archief

© Rob Schlüter - Contact