php - 列出当前类别中的 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 中的所有帖子。
解决方案
如果要显示当前类别和类别 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 类来获取帖子,因此它的参数是相同的。
推荐阅读
- python - 使用 alembic 或烧瓶 (Python) 进行数据库迁移
- sql-server - 有没有办法在 SQL 中获取整个表的列名及其值?
- docker - 连接到新环境时出错:错误:查询通道时出错:14 UNAVAILABLE:无法连接到所有地址
- go - 有什么方法可以取消延迟语句
- wpf - WPF 绑定画笔生成编组错误
- azure - Azure Repos - 允许同事在不推送的情况下提交
- python - 有没有办法在忽略行顺序的情况下比较 Python 中的 CSV?
- python - 即使尝试了所有解决方案,X 轴次要网格线仍然没有显示
- r - 添加序列
- c# - 如何在postgresql中使用c#从文本搜索过滤器中显示dataGrid