如果某个类别中没有帖子,如何隐藏它

时间:2015-11-23 07:42:46

标签: php wordpress

如果类别19中没有帖子,我该如何在html中隐藏产品

<div id='cssmenu'>
    <ul>
        <li class='active'><a href='#'><span>Home</span></a></li>
        <li class='has-sub'><a href='#'><span>Products</span></a>
            <ul >
                <?php query_posts('showposts=5&orderby=date&cat=19'); ?>
                <?php while (have_posts()) : the_post(); ?>
                <a href="<?php the_permalink(); ?>" ><li><?php the_meta(meta_key); ?><?php the_title(); ?> </li></a>
                <?php endwhile; ?>
            </ul>
        </li>
    </ul>
</div>

示例菜单在这里:http://cssmenumaker.com/menu/modern-jquery-accordion-menu

2 个答案:

答案 0 :(得分:3)

您可以从服务器端执行此类操作

<div id='cssmenu'>
  <ul>
    <li class='active'><a href='#'><span>Home</span></a>
    </li>

    <?php query_posts( 'showposts=5&orderby=date&cat=19'); if(have_posts()):?>
    <li class='has-sub'><a href='#'><span>Products</span></a>
      <ul>
        <?php while (have_posts()) : the_post(); ?>
        <a href="<?php the_permalink(); ?>">
          <li>
            <?php the_meta(meta_key); ?>
            <?php the_title(); ?>
          </li>
        </a>
        <?php endwhile; ?>

      </ul>
    </li>
    <?php endif;?>
  </ul>
</div>

使用jQuery检查并按li个元素的数量过滤空产品项并隐藏它们

$('.has-sub').filter(function(){
    return $('ul li',this).length == 0;
}).hide()

答案 1 :(得分:0)

试试这个,

<?php query_posts('showposts=5&orderby=date&cat=19'); ?>
<?php if(have_posts()) { //       <------ check before entering while.
         while (have_posts()) : the_post(); ?>
            <a href="<?php the_permalink(); ?>" ><li><?php the_meta(meta_key); ?><?php the_title(); ?> </li></a>
<?php } endwhile; ?>