【问题标题】:Wordpress multiple ajax filtering for custom post type with two taxonomies具有两种分类法的自定义帖子类型的 Wordpress 多个 ajax 过滤
【发布时间】:2021-01-22 00:11:38
【问题描述】:

我需要有关自定义帖子类型存档页面的帮助。

我想为注册了两个分类法的自定义帖子类型构建三个过滤器。第一个分类是分层的类别,第二个不是分层的标签。

第一个过滤器用于分层分类父元素。非分层分类的第二个过滤器。第三个是分层分类子元素。

我想让第一个过滤器(分层分类父元素)提供第二个(非分层分类元素)和第三个(分层分类子元素),然后显示结果。有什么想法吗?

【问题讨论】:

  • 你试过什么?你的代码在哪里?你看过 FacetWP 插件吗?它完全符合您的要求。

标签: ajax wordpress filtering


【解决方案1】:

首先,我有一个 ul 列表,其中包含第一个分类法的所有父元素。通过单击特定的 li 元素,我调用了一个 ajax 函数,该函数更改了包含父分类的所有子元素的第二个过滤器。

我试图找到一种方法让 get_terms() 在 functions.php 中工作,以便在 ajax 函数中使用它。我通过 ajax javascript 获得变量 $product_type_id 。是一个存储父分类term_id的变量,想用它来获取特定产品类型的子分类。

function getCurrentTerm($product_type_id){

    global $product_type_id;
    
    $my_products = get_terms([
        'taxonomy' => ['awarded_product'],
        'hide_empty' => false,
        'parent' => $product_type_id ,                
        'order' => 'DESC'      
    ]);    
    
    return $my_products;
}


  function set_product_type_filter(){   
    

    $product_type_id = $_POST['product_type_id'];
    if( $_POST['product_type_id']!=''){ 
        $my_product = getCurrentTerm($product_type_id);

    $response['product_type_id'] = $_POST['product_type_id'];
    $response['product']=$my_product;
    echo  json_encode($response);

    wp_reset_postdata();
    wp_die();
    }
  }

  add_action('wp_ajax_set_product_type_filter', 'set_product_type_filter');
  add_action('wp_ajax_nopriv_set_product_type_filter', 'set_product_type_filter');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-17
    • 2023-04-10
    • 1970-01-01
    • 2014-06-22
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    相关资源
    最近更新 更多