wordpress-theming - 木材:如何在 archive.php 中查询单个自定义帖子类型的帖子
问题描述
我正在为 Wordpress 使用 Timber,当我在 archive.php 中使用自定义查询来获取自定义帖子类型的帖子时,它会返回多种帖子类型的帖子。
我在 page.php 中尝试了完全相同的查询,它运行良好。
这是我正在使用的查询:
global $paged;
if (!isset($paged) || !$paged){
$paged = 1;
}
$args = array(
'post_type' => 'horse',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => 20,
'paged' => $paged
);
$context['horses'] = new Timber\PostQuery( $args );
我希望返回所有帖子类型“马”的项目,但其他帖子类型也混入其中。
任何想法为什么会发生这种情况?
我不确定这是否相关,但我还在 StarterSite 类中的 functions.php 中添加了它,以便将我的自定义帖子类型添加到存档页面:
function namespace_add_custom_types( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
// Get all your post types
$post_types = get_post_types();
$query->set( 'post_type', $post_types );
return $query;
}
}
这被添加到名为 的现有函数中__construct
:
add_filter( 'pre_get_posts', array($this, 'namespace_add_custom_types') );
解决方案
你如何在树枝文件中调用它?
{% for post in horses %}
{{ post.name }}
{% endfor %}
另外,您确定帖子类型名称是马而不是马吗?如果“post_type”中包含错误的帖子类型名称,它将显示所有帖子类型。
推荐阅读
- amazon-web-services - 在 MacOS 上安装 aws2 cli 后出现 AWS2 SSO 配置错误
- python - 为什么我的列表理解函数比字符串连接的列表附加函数慢?
- python - 如何将多个 csv 文件合并为一个文件,其中包含 pandas、python 上的特定列?
- c++ - 带有 zip_source_buffer 的 libzip 会导致数据损坏和/或段错误
- c# - 有没有办法可以从脚本中更改另一个对象的材质?
- html - 当元素也悬停时不显示顺风活动样式
- python - 返回经度/纬度坐标集之间的最小距离
- go - 验证 POST JSON 正文匹配结构
- python - 在python中洗牌一个数组
- node.js - 当我尝试在 adonis 中建立关系时出错,我如何确定将在每个关系中返回的列?只返回 id