WordPress.org

Ready to get started?Download WordPress

Forums

How to display archive links by current category (1 post)

  1. earthtojeremy
    Member
    Posted 3 years ago #

    HOW TO (QUITE EASILY) DISPLAY ARCHIVE LINKS FOR THE CURRENT CATEGORY

    This is for people who want single.php, archive.php, and any other page with dynamic content in their WordPress theme to display links to archive pages relevant only to the current category. For instance, if I'm reading a post that's in the category "photography," the archive links in the sidebar menu know this, and will display the archives for other posts in the "photography" category ONLY, not the total archives for the site. There are a lot of different ways to filter archives by category statically, but that's not very helpful when trying to design dynamic, catch-all template pages. The solution which I came up with actually combines a static third-party plug-in with a simple snippet of dynamic php code.

    THE PLUGIN YOU NEED TO DOWNLOAD and activate in your theme for this to work is called Kwebble's Archives for a Category (download and installation instructions are available here.) This is a tiny plug-in that allows you to add the cat parameter to wp_get_archives (why doesn't WordPress allow you to do this by default?!). There are two limitations to this plug-in. 1) You can only filter by category ID number, not name, slug, etc. 2) You have to include the category ID by which you want the archives links to filter manually, in the source code. You can't return the current category being viewed dynamically. This was the big problem for me.

    THE SNIPPET OF CODE I dissected to make this work is this:
    <?php $category = get_the_category(); echo $category[0]->cat_name; ?>

    I came upon this when researching a method for dynamically calling up header graphics unique to each category. That line of code determines the category of the post you're currently viewing, then echoes it.

    HERE IS THE RESULTANT CODE:

    <?php
         $category = get_the_category();
         $params = array(
        	'type'            => 'monthly',
            'cat'             => $category[0]->cat_ID,
            'limit'           => '20',
            'show_post_count' => false,
    	'echo'            => 1 );
         wp_get_archives($params);
    ?>

    I am a coding novice and I honestly don't know the proper verbage to accurately describe what's going on here, so I'll just explain it in layman's terms as I understand it. What I've got here is an array of parameters for wp_get_archives. Typical parameters, but you'll notice the cat param, which only works because I have the above mentioned plug-in installed. You're supposed to type in the number of category you want there, but I've added in that snippet of code to dynamically determine the category of the currently viewed post. The first half $category = get_the_category(); is pasted above the parameters. This I assume is somehow establishing that the current category is THE category. The second half $category[0]->cat_ID is pasted where the static number would be. This is telling the site that whatever the current category is, that's the number that should be printed here. Remove echo, and change cat_name to cat_ID (because the plug-in only recognizes the numeric ID of the category, not the name, remember!)

    BAM! The category ID of the current post is now dynamically printed into the code of the category-filtered archives list!

    It took me about 10 hours of googling but I figured this out. I'm surprised the answer to this isn't prominently listed in this or any other forum. In fact it wasn't, I had to piece together different function concepts to figure this out (Beyond basic HTML/CSS, I am a total programming novice who simply has a penchant for recognizing patterns.) I'm even more surprised that this isn't a basic function of WordPress by default. Perhaps in a future release (wink wink!).

    If anyone has any alternatives for this particular functionality I encourage you to post it here. It seems to me a much sought-after, fundamental function for any magazine-like blog sites with multiple columns, and it's not as simple to pull off as it should be.

    Hope this helps someone.
    - Jeremy
    http://www.earthtojeremy.com

Topic Closed

This topic has been closed to new replies.

About this Topic

  • RSS feed for this topic
  • Started 3 years ago by earthtojeremy
  • This topic is not a support question
  • WordPress version: 3.0.1