显示从页面的自定义字段(WP)中选择的自定义文章类型分类标准
问题描述:
我已经为它创建了自定义文章类型和分类标准。我希望管理员能够在创建新页面(页面)时选择他们希望在页面上显示的分类标准。我创建了一个自定义页面模板,并且有一个条件自定义字段显示可用的分类,当选择该模板时。使用自定义帖子类型用户界面和高级自定义字段插件。显示从页面的自定义字段(WP)中选择的自定义文章类型分类标准
<?php
// this one gets taxonomy custom field
$taxo = get_field('top_to_show');
// and from here on, it outputs the custom post type
$args = array(
'post_type' => 'top_item',
'post_status' => 'publish',
'tops' => $taxo
);
$lineblocks = new WP_Query($args);
if($lineblocks->have_posts()) {
while($lineblocks->have_posts()) {
$lineblocks->the_post();
?>
<div>Custom post type layout html</div>
<?php
}
}
else {
echo '';
}
wp_reset_query(); ?>
现在,当我为页面的分类定制字段选择“术语ID”时,它根本不显示任何内容。当选择“术语对象”时,它会显示所有分类法中的所有文章,而不是特别选择的文章。
如何让它显示特别选定的分类标准?
答
这通过分类检索的帖子,与tax
参数的方法,已被弃用:https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
您应该使用tax_query
代替。假设“顶部”是分类的名称,而您的自定义字段仅返回术语ID:
$args = array(
'post_type' => 'top_item',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'tops',
'field' => 'term_id',
'terms' => $taxo,
),
),
);