![](/img/trans.png)
[英]How to filter list of custom post type with 2 custom taxonomy with ajax in wordpress
[英]Wordpress - how to filter the added custom post through its taxonomy?
我在我的函數中添加了一個新的自定義帖子。php,如何通過其分類對現有帖子進行排序? 目標是在點擊特定類別時過濾現有帖子。 下面是示例代碼:
在函數中添加了自定義帖子。php:
$args = array(
'label' => 'category',
'public' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'show_admin_column' => true,
'hierarchical' => true,
'query_var' => true
);
register_taxonomy('pdf_cat','pdf',$args);
在頁面上 - 獲取分類並將其作為我的類別選項回顯:
<?php
$args = array(
'post_type' => 'pdf',
'orderby' => 'slug',
'order' => 'ASC',
'parent' => 0,
'hide_empty' => false
);
$categories = get_terms([
'taxonomy' => 'pdf_cat',
'hide_empty' => false,
]);
foreach( $categories as $category ){
echo '<option><a class="ctg" href="'. get_category_link( $category->term_id ) .' ">' . $category->name . '</a></option>';
}
?>
在點擊特定類別時通過其分類過濾現有帖子:
<?php
$cat = get_the_category();
$cat = $cat[0];
$catname = get_cat_name($cat->term_id);
$catid = get_cat_ID($catname);
?>
<?php
$paged = get_query_var('paged', 1);
$args = array(
'post_type' => 'pdf',
'paged' => $paged,
'post_type' => 'post',
'cat' => $catid,
);
$query = new WP_Query($args);
global $query_string;
query_posts( $query_string . "&posts_per_page=15&paged=".$paged );
while ( have_posts() ) : the_post()
?>
您需要使用tax_query
$pdf_args = array(
'post_type' => 'pdf',
'paged' => $paged,
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'pdf_cat',
'field' => 'term_id',
'terms' => $catid,
),
),
)
$pdf_data = new WP_Query( $pdf_args );
在此處了解更多詳細信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.