[英]Displaying all posts with custom taxonomy using WP_Query
我正在嘗試顯示具有自定義分類法的自定義帖子類型,但我沒有任何運氣。 什么都沒有出現。 我感謝任何幫助。
崗位類型=大學
自定義分類 slug = country
我想使用 WP_Query 顯示所有國家/地區列表,因為此分類法中有一些自定義字段。 此外,在點擊任何國家時,它應該重定向到包含其詳細信息的國家頁面
下面是我的代碼
<?php
$args = array(
'post_type' => 'university',
'tax_query' => array(
array(
'taxonomy' => 'country'
)
)
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
?>
<a title="<?php the_title(); ?>">
<h3><?php the_title(); ?></h3>
</a>
<?php
}
}
wp_reset_postdata();
?>
我已經修改了你的代碼,請試試看。
<?php
$custom_terms = get_terms('country');
$args = array(
'post_type' => 'university',
'tax_query' => array(
array(
'taxonomy' => 'country',
'field' => 'slug',
'terms' => $custom_terms[0]->slug, // or the category name e.g. Germany
),
)
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
?>
<a title="<?php the_title(); ?>">
<h3><?php the_title(); ?></h3>
</a>
<?php
}
}
wp_reset_postdata();
?>
我們獲取分類法的所有術語,遍歷它們,並觸發指向屬於該術語的每個帖子的標題鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.