列出当前类别的 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 - Category Parameters.
P.S.: get_posts() 内部使用 WP_Query class 来获取帖子,因此其参数相同。
使用此处的代码: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 - Category Parameters.
P.S.: get_posts() 内部使用 WP_Query class 来获取帖子,因此其参数相同。