显示从页面的自定义字段 (WP) 中选择的自定义 post 类型分类法
Show custom post type taxonomy selected from page's custom field (WP)
我已经为它创建了自定义 post 类型和分类法。我希望管理员能够在创建新分类(页面)时选择他们想要在页面上显示的分类。我已经创建了一个自定义页面模板,并且有一个显示可用分类的条件自定义字段,当该模板被 selected 时。为此使用自定义 post 类型 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(); ?>
现在,当我 select "Term ID" 用于页面的分类自定义字段时,它根本不显示任何内容。当我 select "Term Object" 时,它会显示所有分类法中的所有 post,而不是特定的 selected 分类法。
如何让它显示特定选择的分类 posts?
使用 tax
参数按分类检索帖子的方法已弃用:https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
您应该改用 tax_query
。假设 'tops' 是分类法的名称,而您的自定义字段 returns 仅为术语 ID:
$args = array(
'post_type' => 'top_item',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'tops',
'field' => 'term_id',
'terms' => $taxo,
),
),
);
我已经为它创建了自定义 post 类型和分类法。我希望管理员能够在创建新分类(页面)时选择他们想要在页面上显示的分类。我已经创建了一个自定义页面模板,并且有一个显示可用分类的条件自定义字段,当该模板被 selected 时。为此使用自定义 post 类型 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(); ?>
现在,当我 select "Term ID" 用于页面的分类自定义字段时,它根本不显示任何内容。当我 select "Term Object" 时,它会显示所有分类法中的所有 post,而不是特定的 selected 分类法。
如何让它显示特定选择的分类 posts?
使用 tax
参数按分类检索帖子的方法已弃用:https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
您应该改用 tax_query
。假设 'tops' 是分类法的名称,而您的自定义字段 returns 仅为术语 ID:
$args = array(
'post_type' => 'top_item',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'tops',
'field' => 'term_id',
'terms' => $taxo,
),
),
);