如何让wrodpress在分类列表页显示其下子分类文章列表
- 时间:2020-05-17 12:22:02
- 分类:网络文摘
- 阅读:76 次
分享一个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>';
- }
- }
- }
- ?>
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()
- 评论列表
-
- 添加评论