Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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):

<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...

share|improve this question
    
Please, consult the Codex whenever you are at loss with a WordPress function. I'm pretty sure you'll be able to answer your own Question. –  brasofilo Dec 11 '12 at 22:19
    
My bad, the default for hide_empty is true... but what if you explicitly declare it? get_tags(array('hide_empty'=>true)); –  brasofilo Dec 11 '12 at 23:36
    
I had tried that and it still displays the empty tags, I should have explained that in my post. –  ByteMyPixel Dec 11 '12 at 23:44
    
Yes, that's what's called "showing research efforts", it avoids RTM situations ;) In a basic test, get_tags() doesn't return the empty ones for me. In your foreach, print the value of $tag->count. –  brasofilo Dec 11 '12 at 23:51
    
$tag->count shows a count in each, here is a screenshot: link - So it is counting regardless of not being in the current child-category (counts if in the parent-category) –  ByteMyPixel Dec 12 '12 at 0:08
show 2 more comments

1 Answer

up vote 0 down vote accepted

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>';
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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