Intereting Posts
Нестандартная ссылка меню не работает для внешнего сайта Виджет для конкретной категории Невозможно добавить пункты меню в исходное меню 4 Нужна помощь в получении определенного значения из многомерного массива Получение пользовательской переменной VServer / Rootserver / Shared Hosting: несколько установок WordPress, каждый из которых имеет свой уникальный домен? Добавить класс к последним 3 сообщениям в цикле Можно создать подкаталог, проходящий через одну категорию? Сломанные URL-адреса с http-сайтом и https wp-admin _wp_page_template для динамического использования шаблона Как WordPress санирует почтовый контент при сохранении? Или это не так? Почему добавляется .map в конец пути к файлу? Пользовательская тема хочет удалить одну категорию из всех дисплеев Как определить, зарегистрирован ли администратор вне цикла WP set auth cookie с использованием Ajax не сохраняется в браузере

Как я могу скрыть теги на странице дочерней категории, если этот тег не использовался?

У меня есть раздел моего сайта, где я использую дочерние категории и теги для сортировки содержимого.

У меня есть третичная навигация для тегов (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> 

Проблема в том, что в этом списке указано ВСЕ теги, даже если у child-category-b нет сообщений, помеченных tag-a , он будет перечислять tag-a когда на странице child-category-b .

Мне нужен способ скрыть теги / показывать только теги, если они были использованы в этой дочерней категории.

Вот скриншот того, с чем я работаю:
введите описание изображения здесь
нажмите, чтобы увеличить ⤴

Шаблон 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 . '&hellip; <a href="' . get_permalink() . '">More &rsaquo;</a></p> </article>'; ?> <?php endwhile; endif; //wp_reset_postdata(); // to reset the loop ?> </div><!-- [END] feed --> 

И шаблон parent-category и child-category (category-easysteps.php):
Я использую функцию, чтобы заставить child-categories использовать шаблон 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 --> 

Я искал решение, но я в недоумении …

Solutions Collecting From Web of "Как я могу скрыть теги на странице дочерней категории, если этот тег не использовался?"

Я, благодаря помощи сослуживца, у меня теперь есть решение этого 🙂

Третичная навигация для тегов (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>'; } , // 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>'; } , // 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>'; } , // 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>'; } , // 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>'; } , // 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>'; } , // 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>'; } , // 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>'; } , // 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>'; }