I am using Isotope to filter posts on the fly from my archive pages.

I have a Custom Post Type called "art". On the sidebar, I've created links to yearly archives on my sidebar, i.e. 2014, 2013, 2012... etc.

When I am on a yearly archive page, i.e. /art/2012, I want only the terms which exist for this custom-post-type on this year's archive page to appear.

For example, if I am on the date archive of 2009, I only have drawings, not paintings, so I only want the category "Drawings" to show, as clicking "Paintings" will yield zero results (hence the redundancy).

The code I have currently on my sidebar.php (outside the loop) is a modified version that I found here which simply makes wordpress regurgitate all categories across all post types, which is not what I want:

<ul id="filters" class="menu filter">
    <li><a href="#" data-filter="*">All</a></li>

    <?php // Verify if tag/cat is used in archive: https://wordpress.stackexchange.com/a/31981/32287
    $categorys = get_categories(
                    'type' => 'art',
                    'exclude' => '1,5',
                    'hide_empty' => false
    if ($categorys) {
        foreach ($categorys as $category) {
            echo '<li>';
            if($category->count > 0){ // check cat count
                    echo '<a href="#" data-filter=".category-' . $category->slug . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name . '</a></li>';
            } else { // if no posts
        } // end foreach
    } // end if

Can anyone help me write a function which will output the terms that only exist for the yearly date archive page of custom post type "Art"? Been googling for days without avail. :(


You should be able to use what you have and just wrap it with:

<?php if ( is_year( ) ) { ?> **Your Code** <?php } ?>

And changing 'hide_empty' => false to 'hide_empty' => 1 should prevent the cats with no posts from showing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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