How can I hide tags on a child-category page, if that tag has not been used?

I have a section of my site where I’m using child-categories and tags to sort content.

I’m have a tertiary navigation for the tags (templates/nav-tags.php):

Read More
<nav class="side left">
    <ul id="filters">
        <?php
          $tags = get_tags( 
                array( 
                    'hide_empty' => true
                    ) 
                );
            if ($tags) {
                foreach ($tags as $tag) {
                    echo '<li><a id="tag-' 
                        . $tag->slug 
                        .'" href="" title="' 
                        . sprintf( __( "filter post by %s" ), $tag->name ) 
                        . '" ' 
                        . '>' 
                        . $tag->name
                        .'</a></li> ';
                }
            }
        ?>
    </ul>
</nav>

The problem is, this lists ALL of the tags, even if child-category-b doesn’t have any posts tagged in tag-a, it will list tag-a when on the child-category-b page.

I need a way to hide tags/only show tags if they have been used in that child-category.

Here is a screenshot of what I’m working with:
enter image description here
click to enlarge ⤴

The posts template (templates/posts-easy-steps.php):

<div class="feed med no-border" id="sortable-portfolio">

    <?php
        $category = get_the_category();             
            $args = array(
               'post_type' => 'easy_steps',
               'posts_per_page' => 4,
               'category__in' => ($cat),
            );      

        $second_query = new WP_Query( $args );
        if ( $second_query->have_posts() ):
           while( $second_query->have_posts() ) : $second_query->the_post();

           $titlechars = 45; // Character Limit
           $posttitle = get_the_title();
           $modtitle = substr($posttitle, 0, $titlechars);

           $contentchars = 120; // Character Limit
           $postcontent = get_the_excerpt();
           $modcontent = substr($postcontent, 0, $contentchars);

        echo '<article ';
        echo ' ' . post_class('',false) . '  ';
        echo '>';
    ?>

    <?php

        if( get_field('image') ):

            $attachment_id = get_field('image');
            $size = 'customfeatins'; // (thumbnail, medium, large, full or custom size)
            $image = wp_get_attachment_image_src( $attachment_id, $size );

        echo '<a href="' . get_permalink() . '"><img src="' . $image[0] . '" alt="' . get_the_title() .'" width="136" height="90" /></a>';
    ?>

    <?php else : ?>

        <?php echo '<a href="' . get_permalink() . '"><img src="'. get_template_directory_uri() .'/assets/img/content/bf-default.gif" alt="bf-default" width="136" height="90" /></a>' ?>

    <?php endif; ?>

    <?php
        echo '
            <h3><a class="purple" href="' . get_permalink() . '">' . $modtitle .'</a></h3>
            <p class="date">' . get_the_date() .'</p>
            <p>' . $modcontent . '&hellip; <a href="' . get_permalink() . '">More &rsaquo;</a></p>

            </article>';
    ?>

    <?php                              
        endwhile;
        endif;
        //wp_reset_postdata(); // to reset the loop
    ?>

</div><!-- [END] feed -->

And the parent-category and child-category template (category-easysteps.php):
I’m using a function to force child-categories to use the parent-category template

<div class="container" id="wrap" role="document">

<?php get_template_part('templates/masthead'); ?>
<?php get_template_part('templates/header-top-navbar'); ?>


    <section id="main-content" role="main">

        <div class="column-2 left">

            <h1 class="fs-80 border-bottom salmon">Easy Steps 
            <?php
            if ( is_category() ) {
                $category = get_category( $cat );
                echo ' - <span class="fs-50"> '.$category->cat_name.' </span>';
                } else {
            } ?>
            </h1>

            <?php edit_post_link('edit this entry', '<p>', '</p>'); ?>

            <div class="easytags horiz">
            <?php get_template_part('templates/easy-cats'); ?> 
            </div>
            <?php get_template_part('templates/nav', 'tags'); ?> 
            <?php get_template_part('templates/posts-easy-steps'); ?>

        </div><!-- [END] left -->

            <?php if (hchw_sidebar()) : ?>
                <?php get_template_part('templates/sidebar', 'right'); ?>
            <?php endif; ?>

    <div class="clear"></div>
    </section>


</div> <!-- [END] container -->

I’ve been searching for a solution but I’m at a loss…

Related posts

Leave a Reply

1 comment

  1. Yay, thanks to the help of a co-worker, I now have a solution to this 🙂

    Tertiary navigation for the tags (templates/nav-tags.php):

    // only load tags from the current page
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
       'post_type' => 'easy_steps',
       'paged' => $paged,
       'category__in' => ($cat),
    );
    $second_query = new WP_Query( $args );
    
    global $wpdb;
    $tags = array();
    $terms = array();
    foreach ($second_query->posts as $post)
    {
        $post_id = $post->ID;
        // get post tags for post
        $taxonomy = $wpdb->get_results(
            $wpdb->prepare("
                SELECT *
                FROM `hchw_wp2_term_relationships` tr
                INNER JOIN `hchw_wp2_term_taxonomy` tt ON tt.term_taxonomy_id=tr.term_taxonomy_id
                INNER JOIN `hchw_wp2_terms` t on t.term_id=tt.term_id
                WHERE tt.taxonomy='post_tag' AND tr.object_id=".(int)$post_id
            )
        );
        foreach ($taxonomy as $tag)
        {
            $term = $tag->slug;
            if (!isset($$term))
            {
                // populate tag
                $$term = new stdClass;
                $$term->term_id=$tag->term_id;
                $$term->name=$tag->name;
                $$term->slug=$tag->slug;
                $$term->term_group=$tag->term_group;
                $$term->term_taxonomy_id=$tag->term_taxonomy_id;
                $$term->taxonomy=$tag->taxonomy;
                $$term->description=$tag->description;
                $$term->parent =$tag->parent;
                $$term->count='1';
    
                $tags[] = $$term;
                $terms[$term] = $counter; // so I know what offset the tag is at in case I need to increase the count
                $counter++;
            }
            else
                $tags[$terms[$term]]->count += 1;
        }
    }
    
    if (count($tags) > 0)
    {
        foreach ($tags as $tag)
            echo '<li id="tag-' . $tag->slug .'"><a id="tag-' . $tag->slug .'" href="" title="' . sprintf( __( "filter posts by %s" ), $tag->name ) . '" ' . '>' . $tag->name.'</a></li>';
    }