[英]How to query a custom post type taxonomy to create a taxonomy list for a template page
我正在嘗試構建archive-custom.php頁面以顯示選項卡列表。 每個自定義帖子類型都是與分類法相關聯的“消息”,該分類法將它們分組並以“系列”形式組織它們。 分類包含有關每個系列的信息,包括“系列類型”和每個系列的圖形。
目標是頁面顯示“main”類型的所有“系列”的列表,其中包含圖形和分類名稱。 單擊“系列”將帶您進入“系列”頁面,其中包含所有“消息”的列表
我有一個查詢返回我想要的大部分信息,此時它只返回重復項,因為我的查詢是錯誤的。 它返回每個“消息”的“系列”,所以如果有4個“消息”,我將獲得4次“系列”。 我知道查詢需要改變。
我還有一個問題是返回分類法的名稱。 在下面的代碼中,我讓它返回2種不同的方式 - 一種可以工作,但在鏈接中返回分類名稱,這是不必要的。 另一個只返回“數組”,因為我的語法錯誤,我在Wordpress codex中找不到一個例子。
我對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;
?>
它非常接近於我想做的事情,再次為每個“消息”獲取“系列”而不僅僅是“main”類型中所有“系列”的列表。 很想知道如何正確地獲得“名字”。 謝謝。
對於您遇到的第一個問題:
$args = array(
'post_type' => 'messages',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'cpt_series',
'field' => 'series_type',
'terms' => 'main'
)
)
);
'field' => 'series_type'
- 您在此處根據搜索和選項name
: term_id
, name
, slug
或term_taxonomy_id
。 轉到數據庫並找到表wp_terms。 你可能會看到這些價值觀。 所以我假設這些行應該類似於:
array(
'taxonomy' => 'cpt_series',
'field' => 'slug',
'terms' => 'main'
)
您可以針對初始查詢嘗試此元查詢。
$args = array(
'post_type' => 'messages',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'series_type',
'value' => 'main',
'compare' => 'IN',
)
)
);
通過直接查詢分類法發現了如何執行此操作。
$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.';
}
謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.