如何使用AJAX按税种过滤帖子

时间:2013-04-20 作者:Emily

我发现this post 这描述了如何使用Ajax过滤分类帖子,效果很好,但我也想以同样的方式过滤我的自定义分类法,但我无法让它正常工作。它显示了所有帖子,而不仅仅是分类法中的帖子。

我知道菜单需要改成get_the_terms 而不是get_the_categories 但是我特别需要关于jQuery函数和底部php函数的更改的帮助。我尝试添加tax_query 调用我的分类法,但它仍然没有显示正确的帖子。有人能帮我指出正确的方向吗?

3 个回复
SO网友:Emily

我想出来了!以下是我使用的代码:

添加到functions.php:

add_action( \'wp_ajax_nopriv_load-filter2\', \'prefix_load_term_posts\' );
add_action( \'wp_ajax_load-filter2\', \'prefix_load_term_posts\' );
function prefix_load_term_posts () {
        $term_id = $_POST[ \'term\' ];
            $args = array (
            \'term\' => $term_id,
            \'posts_per_page\' => -1,
            \'order\' => \'DESC\',
                 \'tax_query\' => array(
                  array(
                      \'taxonomy\' => \'yourtaxonomyhere\',
                      \'field\'    => \'id\',
                      \'terms\'    => $term_id,
                      \'operator\' => \'IN\'
                      )
                  )
             );

        global $post;
        $myposts = get_posts( $args );
        ob_start (); ?>

        <ul class="list">
        <?php foreach( $myposts as $post ) : setup_postdata($post); ?>
            <li><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php echo get_post_meta($post->ID, \'image\', $single = true); ?></a><br />
             <?php the_title(); ?></li>
       <?php endforeach; ?>
        </ul>

       <?php wp_reset_postdata(); 
       $response = ob_get_contents();
       ob_end_clean();
       echo $response;
       die(1);
}
jQuery脚本:

<script>
function term_ajax_get(termID) {
    jQuery("a.ajax").removeClass("current");
    jQuery("a.ajax").addClass("current"); //adds class current to the category menu item being displayed so you can style it with css
    jQuery("#loading-animation").show();
    var ajaxurl = \'http://yourdomain.com/wp-admin/admin-ajax.php\';
    jQuery.ajax({
        type: \'POST\',
        url: ajaxurl,
        data: {"action": "load-filter2", term: termID },
        success: function(response) {
            jQuery("#category-post-content").html(response);
            jQuery("#loading-animation").hide();
            return false;
        }
    });
}
</script>
我没有使用函数来列出类别,我只是分别列出每个类别。将数字替换为术语的ID:

<ul class="nav">
     <li id="term-166"><a class="yourtermname ajax" onclick="term_ajax_get(\'166\');" href="#">Your Term Name</a></li>
     <li id="term-354"><a class="yourtermname ajax" onclick="term_ajax_get(\'354\');" href="#">Your Term Name</a></li>
</ul>
此外,如果要筛选标记而不是术语,请替换:

  • \'term\' 具有\'tag__in\',
  • $term_id 具有$tag_id\'taxonomy\' => \'yourtaxonomyhere\' 到\'taxonomy\' => \'post_tag\'.

SO网友:erwanpia

我建议您使用一个短代码来显示您选择的分类法:创建一个类来声明短代码并调用此函数

  public function shortcode($atts)
{
 extract(shortcode_atts( array(
    \'data\' => \'taxonomy\',
    \'taxonomy\' => \'category\',
    //get_terms arguments
    \'parent\' => 0, //only get top level terms by default
    \'exclude\'=>\'\',
    \'type\'=>\'radio\' // checkbox,radio
    ), $atts,\'astSearchInput\' ));

$arrStr =array();
$arrStr[]= "<div class=\'astSearchInput " . $taxonomy. "\' taxonomy=\'" .$taxonomy. "\'>"  ;
if ($type=="checkbox" || $type=="radio")
{
    if ($data=="taxonomy")
        {
        //echo $datatata;
        $arrValues=get_terms($taxonomy, array("parent"=>$parent, "exclude"=>$exclude)); 
        }


     if ($type=="checkbox")$arrStr[]= $this->inputCheckBox($arrValues,$atts);
     if ($type=="radio")$arrStr[]= $this->inputRadio($arrValues,$atts);
}
$arrStr[]= "</div>";
$str=join("\\n",$arrStr);
return $str  ;
}




    function inputCheckBox($arrValues,$attr)
{
    $arrStr =array();
    $arrStr[]=\'<div class="formcb">\';
    foreach($arrValues as $k=>$term)
        {
            $title=$term->name; //$term->description 
            //  print_r($term);
            $id="cb" . $term->term_id;
            $arrStr[]=\'<div class="cb"><input class="astInpuntTerm astcheckBox" type="checkbox" id="\' . $id  .\'" value="\' . $term->term_id . \'" ><label for="\' . $id . \'">\' . $title. \'</label></div>\';
        }
    $arrStr[]=\'</div>\'; 
    $str=join("\\n",$arrStr);    
        return $str;
}

http://www.webmasterbulletin.net/wordpress-ajax-taxonomy-search-shortcode

SO网友:Camil

我也有类似的问题。

代码很好,但需要稍加修改才能工作。

            $args = array (
        \'term\' => $term_id,
        \'posts_per_page\' => -1,
        \'order\' => \'DESC\',
             \'tax_query\' => array(
              array(
                  \'taxonomy\' => \'yourtaxonomyhere\',
                  \'field\'    => \'id\',
                  \'terms\'    => $term_id,
                  \'operator\' => \'IN\'
                  )
              ),
               \'post_type\' => \'yourcustomposttype\', // <== this was missing
\'posts_per_page\' => 10,
\'order\' => \'DESC\'
         );

结束