如何让wrodpress在分类列表页显示其下子分类文章列表

  • 时间:2020-05-17 12:22:02
  • 分类:网络文摘
  • 阅读:76 次

分享一个wordpress技巧,只需简单的一段代码,就可以让 WordPress 像 CMS 站点那样在分类列表页面调用显示其下相关子分类的各自文章列表,如果当前分类下不存在子分类,则显示该分类下的文章列表。效果如下图:

让wrodpress在分类列表页显示其下子分类文章列表

具体方法:将下列代码按需调整后添加到wordpress主题的分类目录模板文件(category.php)中即可。

  1. <?php
  2.     global $cat;
  3.     $cats = get_categories(array(
  4.         'child_of' => $cat,
  5.         'parent' => $cat,
  6.         'hide_empty' => 0
  7.     ));
  8.     $c = get_category($cat);
  9.     if(emptyempty($cats)){
  10. ?>
  11. <div class="item">
  12.     <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  13.     <div class="post">
  14.         <h2><a title="<?php the_title(); ?>" href="http://uuxn.com/show-wordpress-subcategories-article-in-category/<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
  15.         <p><?php the_excerpt(); ?></p>
  16.         <p><a href="http://uuxn.com/show-wordpress-subcategories-article-in-category/<?php the_permalink(); ?>">全文阅读>></a></p>
  17.         <div class="meta"><?php the_time('Y-m-d'); ?> | 标签: <?php the_tags('', ' , ', ''); ?></div>
  18.     </div>
  19.     <?php endwhile; ?>
  20.     <?php else: ?>
  21.         <div class="post"><p>文章稍后更新</p></div>
  22.     <?php endif; ?>
  23. </div>
  24. <div class="navigation">
  25.     <span class="alignleft"><?php next_posts_link('&laquo; Older posts') ?></span>
  26.     <span class="alignright"><?php previous_posts_link('Newer posts &raquo;') ?></span>
  27. </div>
  28. <?php
  29. }else{
  30.     foreach($cats as $the_cat){
  31.         $posts = get_posts(array(
  32.             'category' => $the_cat->cat_ID,
  33.             'numberposts' => 10,
  34.         ));
  35.         if(!emptyempty($posts)){
  36.             echo '
  37.             <div class="item cat_item">
  38.                 <div class="item_title"><h2><a title="'.$the_cat->name.'" href="http://uuxn.com/show-wordpress-subcategories-article-in-category/'.get_category_link($the_cat).'">'.$the_cat->name.'</a></h2></div>
  39.                 <ul class="box_list">';
  40.                     foreach($posts as $post){
  41.                         echo '<li><span class="alignright">'.mysql2date('Y-m-d', $post->post_date).'</span>
  42.                         <a title="'.$post->post_title.'" href="http://uuxn.com/show-wordpress-subcategories-article-in-category/'.get_permalink($post->ID).'">'.$post->post_title.'</a></li>';
  43.                     }
  44.                 echo '</ul>
  45.             </div>';
  46.         }
  47.     }
  48. }
  49. ?>
推荐阅读:
Top Mobile Technology Trends for Bloggers  The Vital Common Sense Marketing Strategy You May Have Forgotten  The 7 Features of a Successful Product Demo Video  Top Facebook Marketing Partner Comments On Facebook’s Newest Vid  Animoto Adds Square Video for Increased Social Media and Mobile   4 Things You Need to Know When Funding Your Freelance Business  How Dedicated Servers Boost the Performance of A Business Blog  Irish Travel Blogger Visits Every Country In The World, Just In   How to Build an Online Store on WordPress Using WooCommerce  C++ Coding Reference: is_sorted_until() and is_sorted() 
评论列表
添加评论