首页 > 解决方案 > 列出当前类别中的 wordpress 帖子和

问题描述

从这里使用此代码:http ://www.snilesh.com/resources/wordpress/wordpress-recent-posts-from-current-same-category/

<h3>Recently Post From Same Category</h3>
<ul>
<?php
global $post;
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;
$myposts = get_posts(array('numberposts' => 5, 'offset' => 0,
'category__in' => array($category), 'post__not_in' =>
array($post->ID),'post_status'=>'publish'));
foreach($myposts as $post) :
setup_postdata($post);
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?></a>
</li>
<?php endforeach; ?>
<?php wp_reset_query(); ?>
</ul>

我能够显示当前类别中的所有帖子。我想显示的是当前类别和类别 186 中的所有帖子。

标签: phpwordpressposts

解决方案


如果要显示当前类别类别 186中的帖子,请category__and改用:

$myposts = get_posts(array(
    'numberposts' => 5,
    'offset' => 0,
    'category__and' => array($category, 186),
    'post__not_in' => array($post->ID),
    'post_status'=>'publish'
));

有关更多详细信息:WP_Query - 类别参数

PS:get_posts()内部使用 WP_Query 类来获取帖子,因此它的参数是相同的。


推荐阅读