[英]Show custom post type taxonomy selected from page's custom field (WP)
我为它创建了一个自定义的帖子类型和分类。 我希望管理员能够在创建新的(页面)时选择他们想要在页面上显示的分类。 我已经创建了一个自定义页面模板,并且在选择该模板时,有一个条件自定义字段显示可用的分类。 使用自定义帖子类型UI和高级自定义字段插件。
<?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
代替。 假设'tops'是分类法的名称,并且您的自定义字段仅返回Term ID:
$args = array(
'post_type' => 'top_item',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'tops',
'field' => 'term_id',
'terms' => $taxo,
),
),
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.