WordPress调用同分类下面的相关文章内容

按照常规的,我们网站的相关文章是调用的TAG相关的或者是最新的文章内容。这里我们希望调用当前文章同分类下面的文章。

<?php 
/* 
single page?show current category articles 
*/ 
?> 
<?php 
if ( is_single() ) : 
global $post; 
$categories = get_the_category(); 
foreach ($categories as $category) : 
?> 
<li class="widget widget_recent_entries" id="<?php $category->term_id;?>-posts"> 
<h2 class="widgettitle"><?php echo $category->name; ?></h2> 
<ul> 
<?php 
$posts = get_posts('numberposts=5&category='. $category->term_id); 
foreach($posts as $post) : 
?> 
<li> 
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
</li> 
<?php endforeach; ?> 
</ul> 
</li> 
<?php 
endforeach; endif ; ?> 
<?php 
/* 
end show current category articles 
*/ 
?> 

根据需要调整样式和调用数量。


【AD】丽萨主机英国VPS推荐 英国原生IP双ISP家宅主机

【AD】美国洛杉矶/香港/日本VPS推荐,回程电信CN2 GIA线路,延迟低、稳定性高、免费备份_搬瓦工