J'ai une section de mon site où j'utilise des catégories et des balises enfants pour trier le contenu.
J'ai une navigation tertiaire pour les 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>
Le problème est que ceci liste TOUTES les balises, même si child-category-b
ne contient aucun article étiqueté dans tag-a
, il listera tag-a
à la page child-category-b
.
J'ai besoin d'un moyen de masquer les balises/afficher uniquement les balises si elles ont été utilisées dans cette catégorie enfant.
Voici une capture d'écran de ce avec quoi je travaille:
Cliquez pour agrandir ⤴
Le modèle posts
(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 -->
Et le modèle parent-category
et child-category
(category-easysteps.php):
J'utilise une fonction pour forcer child-categories
à utiliser le modèle parent-category
<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 -->
Je cherchais une solution mais je suis perdue ...
Oui, grâce à l'aide d'un collègue, j'ai maintenant une solution à cela :)
Navigation tertiaire pour les 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>';
}