wordpress获取当前分类id调用多个子分类列表
我们在做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,非常方便。
上面部分是没有子分类,的文章列表调用,下面部分是调用分类列表文章。
希望对大家有所帮助!
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END


















