如何让wrodpress在分类列表页显示其下子分类文章列表
- 时间:2020-05-17 12:22:02
- 分类:网络文摘
- 阅读:127 次
分享一个wordpress技巧,只需简单的一段代码,就可以让 WordPress 像 CMS 站点那样在分类列表页面调用显示其下相关子分类的各自文章列表,如果当前分类下不存在子分类,则显示该分类下的文章列表。效果如下图:

具体方法:将下列代码按需调整后添加到wordpress主题的分类目录模板文件(category.php)中即可。
- <?php
- global $cat;
- $cats = get_categories(array(
- 'child_of' => $cat,
- 'parent' => $cat,
- 'hide_empty' => 0
- ));
- $c = get_category($cat);
- if(emptyempty($cats)){
- ?>
- <div class="item">
- <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
- <div class="post">
- <h2><a title="<?php the_title(); ?>" href="http://uuxn.com/show-wordpress-subcategories-article-in-category/<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
- <p><?php the_excerpt(); ?></p>
- <p><a href="http://uuxn.com/show-wordpress-subcategories-article-in-category/<?php the_permalink(); ?>">全文阅读>></a></p>
- <div class="meta"><?php the_time('Y-m-d'); ?> | 标签: <?php the_tags('', ' , ', ''); ?></div>
- </div>
- <?php endwhile; ?>
- <?php else: ?>
- <div class="post"><p>文章稍后更新</p></div>
- <?php endif; ?>
- </div>
- <div class="navigation">
- <span class="alignleft"><?php next_posts_link('« Older posts') ?></span>
- <span class="alignright"><?php previous_posts_link('Newer posts »') ?></span>
- </div>
- <?php
- }else{
- foreach($cats as $the_cat){
- $posts = get_posts(array(
- 'category' => $the_cat->cat_ID,
- 'numberposts' => 10,
- ));
- if(!emptyempty($posts)){
- echo '
- <div class="item cat_item">
- <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>
- <ul class="box_list">';
- foreach($posts as $post){
- echo '<li><span class="alignright">'.mysql2date('Y-m-d', $post->post_date).'</span>
- <a title="'.$post->post_title.'" href="http://uuxn.com/show-wordpress-subcategories-article-in-category/'.get_permalink($post->ID).'">'.$post->post_title.'</a></li>';
- }
- echo '</ul>
- </div>';
- }
- }
- }
- ?>
5 Easy Ways On How To Build An Authority Website How to Protect Your WordPress Site From Hackers Top 10 Relationship Blogs With the Best Pieces of Advice in 2020 How to Construct Binary Search Tree from Preorder Traversal in P Dynamic Programming Algorithm to Compute the Block Sum in a Matr Smallest Multiple Algorithm using Bruteforce or GCD/LCM How many different ways can £2 be made using any number of coins Compute Factorial Digit Sum: Find the sum of the digits in the n Compute the Maximum Integer Right Triangles Solutions Power Digit Sum: What is the sum of the digits of the number 2^1
- 评论列表
-
- 添加评论