我们在做wordpress网站的时候,常常需要调用多个子分类的文章列表,如果都是指定id 来进行分类文章调用,就会比较繁琐,也不利于后期更新。
今天给大家介绍一个获取多个分类列表的方法:
<?php global $cat; $cats = get_categories(array( 'child_of' => 1, 'parent' => $cat, 'hide_empty' => 0 )); $c = get_category($cat); if(empty($cats)){ ?> <div class=item> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <?php endwhile; ?> <?php else: ?> <?php endif; ?> </div> <?php ?> <?php }else{ foreach($cats as $the_cat){ $posts = get_posts(array( 'category' => $the_cat->cat_ID, 'numberposts' => 10, )); if(!empty($posts)){ echo ' <div class=item2>'; foreach($posts as $post){ $full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full'); echo '<div class=column5> <div class=text9> <div class=p10> <a href='.get_permalink($post->ID).'><img src='.$full_image_url[0].'/></a></div> <div class=text10>'.mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 150,...).'</div> <div class=more4><a href='.get_permalink($post->ID).'>了解更多</a></div> </div> </div>'; } echo ' </div>'; } } } ?>
通过这个代码,分类可以自动获取,也可以手动输入分类id,非常方便。上面部分是没有子分类,的文章列表调用,下面部分是调用分类列表文章。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。