wordpress post过滤器使用Ajax

时间:2017-04-10 10:19:18

标签: jquery ajax wordpress wordpress-theming

我创建了一个名为products的新帖子类型,并为此商品创建了一个名为product_cat的新分类。

register_taxonomy( 'product_cat', array( 'product' ), $args );

在product_cat中,我添加了汽车,船舶,船只,公共汽车等。

现在我使用以下代码列出所有产品。

 args=array(
        'post_type'=>'product',
        'post_per_page'=>'10');

        $loop=new WP_Query($args);

        while($loop->have_posts()) : $loop->the_post();

        the_title();
        the_content();

        endwhile;

我想现在制作产品过滤器。为此我添加了以下html

<label class="type-label">car<input type="checkbox" class="type-box" value="car"/></label>
<label class="type-label">bus<input type="checkbox" class="type-box" value="bus"/></label>  

我需要的是,当有人点击汽车然后汽车产品只需要显示。

我知道用于获取复选框值的jquery代码。我知道如何list car product

 $args=array(
            'post_type'=>'product',
            'post_per_page'=>'10',
            'tax_query' => array(
                array(
                'taxonomy' => 'product_cat',
                'field'    => 'slug',
                'terms'    => 'car',
                    ),),);
$loop=new WP_Query($args); 

但请使用ajax显示相应的结果。那是当有人点击汽车复选框时,只需要显示汽车类别中的产品。

请任何人可以提供帮助并举一个简单的例子

1 个答案:

答案 0 :(得分:0)

首先你必须在wordpress中添加一个AJAX Action,就像这样: https://codex.wordpress.org/AJAX_in_Plugins

在php函数中,您可以从客户端获取参数,它可能是您要显示的分类ID。 获得并检查所需的所有参数后,您就可以执行查询。

对于客户的异议我建议您阅读: https://premium.wpmudev.org/blog/load-posts-ajax/

希望这能帮到你