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

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

分享一个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. ?>
推荐阅读:
The Ultimate LinkedIn Cheat Sheet  Amazon Email Scam Has Consumers On High Alert  Department Of Justice Announces ‘Hack The Army’ Program  7 Things You Must Do After Installing Your WordPress Site  New Report Discovers Disconnect Between Retailers And Social Med  Counting the Prime Arrangements  The Minimum Absolute Difference Algorithm of an Array  Implement the Depth First Search Algorithm in Graph using Simple  Beginner’s Introduction to PHP Memcached  Using the Regular Expression to Replace External Links in WordPr 
评论列表
添加评论