阿里云服务器

在做WordPress cms主题中常常需要在分类页中调用子分类和分类列表,如果不存在子分类就显示文章列表。

55.JPG

在WordPress主题文件category.php加入以下代码即可实现,

具体样式根据网站结构自行调整。

<?php
    global $cat;
    $cats = get_categories(array(
        'child_of' => $cat,
        'parent' => $cat,
        'hide_empty' => 0
    ));
    $c = get_category($cat);
    if(empty($cats)){
?>

   <div class="infoList">
      <ul class="infoListUL">
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <li> <a href="<?php the_permalink(); ?>" class="articleTitle fl"  target="_blank"><?php the_title(); ?></a>  </li>
    <?php endwhile; ?>
    <?php else: ?>
</ul></div>
    <?php endif; ?>
</div>
      <div class="dede_pages">
        <ul class="pagelist">
<?php pagenavi(); ?>
        </ul>
      </div>

<?php
}else{
    foreach($cats as $the_cat){
        $posts = get_posts(array(
            'category' => $the_cat->cat_ID,
            'numberposts' => 10,
        ));
        if(!empty($posts)){
            echo '
            <div class="typeBox">
               <h2 class="zuojia_title"><a title="'.$the_cat->name.'" href="'.get_category_link($the_cat).'">'.$the_cat->name.'</a></h2><a href="'.get_category_link($the_cat).'" class="more">更多>> </a>  
                <ul class="typeList1">';
                    foreach($posts as $post){
                        echo '<li>
                        <a title="'.$post->post_title.'" href="'.get_permalink($post->ID).'">'.$post->post_title.'</a></li>';
                    }
                echo '</ul>
            </div>';
        }
    }
}
?>
相关阅读: