我正在显示包含以下代码的菜单:
<!-- Subcategory menu from current category -->
<ul class="sub-menu">
<?php
if (is_home()) {
wp_list_categories('orderby=id&title_li=&depth=1');
}
else{
$category = get_the_category();
$cat_term_id = $category[0]->term_id;
$cat_category_parent = $category[0]->category_parent;
$listcat = wp_list_categories('echo=0&child_of='.$cat_category_parent.'&title_li=&orderby=order&order=ASC');
$listcat = str_replace("cat-item-".$cat_term_id, "cat-item-".$cat_term_id." current-cat", $listcat);
if ( in_category( $cat_term_id ) || post_is_in_descendant_category( $cat_term_id )) {
echo $listcat;
}
}
?>
并且该菜单的每个li都显示帖子标题:
<!-- Post list from current category -->
<div class="menu_list">
<ul id="submenu_productos" class="clearfix">
<?php
$IDOutsideLoop = $post->ID;
while( have_posts() ) {
the_post();
foreach( ( get_the_category() ) as $category )
$my_query = new WP_Query('category_name=' . $category->category_nicename . '&orderby=date&order=DESC&showposts=100');
if( $my_query ) {
while ( $my_query->have_posts() ) {
$my_query->the_post(); ?>
<!-- this line to hightlight current post in the category page -->
<li<?php if ( $post->ID == $wp_query->post->ID ) { echo ' class="test"'; } else {} ?>>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
</li>
<?php
}
}
}
?>
</ul>
</div>
菜单效果很好,它显示如下:
问题在于,当我有一个属于该类别/子类别的帖子时,它也会显示在列表中...我想隐藏它只有一个项目:
当子类别只有一个帖子时,有没有办法隐藏它?
PD:请不要告诉我把它放在wordpress.stackexchange.com上,我总是张贴在那里没有答案,这里的人总是帮助我...答案 0 :(得分:1)
您可以使用以下方式计算子菜单中的项目数:
$count = $my_query->post_count;
我建议在if条件中添加它:
if( $my_query && $my_query->post_count > 1)
那应该有用