繁体   English   中英

列出当前类别中的wordpress帖子,以及

[英]list wordpress posts from current category and

从此处使用此代码: 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中的所有帖子。

如果要显示当前类别类别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类来获取帖子,因此其参数相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM