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:
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 . '… <a href="' . get_permalink() . '">More ›</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…
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):