[英]How to query a custom post type taxonomy to create a taxonomy list for a template page
I'm trying to build the archive-custom.php page to display a tabbed list. 我正在尝试构建archive-custom.php页面以显示选项卡列表。 Each custom post type is a "message" tied to a taxonomy that groups and organizes them in a "series". 每个自定义帖子类型都是与分类法相关联的“消息”,该分类法将它们分组并以“系列”形式组织它们。 The taxonomy contains the information about each series, including the "series-type" it is and a graphic for each series. 分类包含有关每个系列的信息,包括“系列类型”和每个系列的图形。
The goal is that the page displays a list of all the "series" of the type "main" with a graphic and taxonomy name. 目标是页面显示“main”类型的所有“系列”的列表,其中包含图形和分类名称。 Clicking on a "series" will take you to that "series" page with a list of all "messages" 单击“系列”将带您进入“系列”页面,其中包含所有“消息”的列表
I have gotten a query to return most of the information that I want, at this point it just returning duplicates, because my query is wrong. 我有一个查询返回我想要的大部分信息,此时它只返回重复项,因为我的查询是错误的。 It returning the "series" for each "message" so if there are 4 "messages" I'm getting that "series" 4 times. 它返回每个“消息”的“系列”,所以如果有4个“消息”,我将获得4次“系列”。 I know the query needs to change. 我知道查询需要改变。
I also was having an issue returning the name of the taxonomy. 我还有一个问题是返回分类法的名称。 In the code below I have it returning 2 different ways - one works but returns the taxonomy name inside a link, which is unnecessary. 在下面的代码中,我让它返回2种不同的方式 - 一种可以工作,但在链接中返回分类名称,这是不必要的。 The other just returns "array" because my syntax is wrong, and I couldn't find an example in the Wordpress codex. 另一个只返回“数组”,因为我的语法错误,我在Wordpress codex中找不到一个例子。
I'm not very familiar with Wordpress queries, but I feel like I have a duplicate query in here that is unnecessary. 我对Wordpress查询不是很熟悉,但我觉得我在这里有一个重复的查询是不必要的。
<?php
//this initial query is returning the results for each message instead of returning just each series in the type "main"
$args = array(
'post_type' => 'messages',
'post_status' => 'publish',
//thanks to dmarco for pointing out this should use the meta_query instead
//not certain how to meta_query the attached taxonomy
'tax_query' => array(
array(
'taxonomy' => 'cpt_series',
'field' => 'series_type',
'terms' => 'main'
)
)
);
$series = new WP_Query( $args );
if( $series->have_posts() ) :
?>
<ul>
<?php
while( $series->have_posts() ) :
$series->the_post();
//get the custom taxonomy fields and assign them to var
//this query is to access the custom post type fields.
$types = get_terms( array(
'taxonomy' => 'cpt_series',
'hide_empty' => true,
));
foreach($types as $type) {
$image = get_field('series_graphic', 'cpt_series_' . $type->term_id . '' );
$seriesLink = get_term_link($type->term_id, $taxonomy = 'cpt_series');
//This title doesn't work - syntax is wrong
$seriesTitle = wp_get_post_terms('name');
if ( has_term( $type->term_id, 'cpt_series')) {
$seriesGraphic = $image['url'];
}
}
?>
<li>
<a href="<?=$seriesLink?>">
<?php
echo '<img src="' . $seriesGraphic . '" />';
echo $seriesTitle;
$seriesTitle = the_terms($post->ID, 'sermon_series', '');
?>
</a>
</li>
<?php
endwhile;
wp_reset_postdata();
?>
</ul>
<?php
else :
esc_html_e( 'No series available!', 'text-domain' );
endif;
?>
It's very close to doing what I want, again just getting the "series" for each "message" instead of just a list of all the "series" in the type "main". 它非常接近于我想做的事情,再次为每个“消息”获取“系列”而不仅仅是“main”类型中所有“系列”的列表。 Would love to know how to properly get the 'name' returned as well. 很想知道如何正确地获得“名字”。 Thanks. 谢谢。
For the first problem you have: 对于您遇到的第一个问题:
$args = array(
'post_type' => 'messages',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'cpt_series',
'field' => 'series_type',
'terms' => 'main'
)
)
);
'field' => 'series_type'
- you name here by what you search and options are: term_id
, name
, slug
or term_taxonomy_id
. 'field' => 'series_type'
- 您在此处根据搜索和选项name
: term_id
, name
, slug
或term_taxonomy_id
。 Go to database and find table wp_terms. 转到数据库并找到表wp_terms。 There you may see those values. 你可能会看到这些价值观。 So I assume these lines should look something like: 所以我假设这些行应该类似于:
array(
'taxonomy' => 'cpt_series',
'field' => 'slug',
'terms' => 'main'
)
You can try this meta query for your initial query. 您可以针对初始查询尝试此元查询。
$args = array(
'post_type' => 'messages',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'series_type',
'value' => 'main',
'compare' => 'IN',
)
)
);
Discovered how to do this by querying the taxonomy directly instead. 通过直接查询分类法发现了如何执行此操作。
$terms = get_terms(array(
'taxonomy' => 'cpt_series',
'hide_empty' => true,
'meta_query' => array(
array(
'key' => 'series_type',
'value' => 'main',
'compare' => 'LIKE'
),
),
));
if ( ! empty( $terms ) ) {
echo '<ul>';
foreach ( $terms as $term ) {
$image = get_field('series_graphic', 'cpt_series_' . $term->term_id . '' );
$seriesLink = get_term_link($term->term_id, $taxonomy = 'cpt_series');
$seriesGraphic = $image['url'];
?>
<li>
<a href="<?=$seriesLink?>">
<img src="<?=$seriesGraphic?>" />
<?=$term->name?>
</a>
</li>
<?php
}
echo '</ul>';
} else {
echo 'No term found.';
}
Thanks. 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.