WordPress Ajax 自定义分类法

4

我目前拥有一个WordPress网站,其中包含自定义文章类型和自定义分类法(jobs_category)。

我已经创建了从此分类法中列出类别的列表:

<?php
  $taxonomy = 'jobs_category';
  $tax_terms = get_terms($taxonomy);
?>
<ul>
  <?php
  foreach ($tax_terms as $tax_term) {?>
    <li id="cat-<?php echo $tax_term->term_id; ?>">
     <a href="#<?php //echo esc_attr(get_term_link($tax_term, $taxonomy)); ?>" class="<?php echo $tax_term->slug; ?> ajax" onclick="cat_ajax_get('<?php echo $tax_term->term_id; ?>');" title="<?php echo $tax_term->name;?>"><?php echo $tax_term->name; ?></a>
    </li>
  <? } ?>
</ul>

我已经在我的函数文件中使用了以下内容:
    add_action( 'wp_ajax_nopriv_load-filter', 'prefix_load_cat_posts' );
add_action( 'wp_ajax_load-filter', 'prefix_load_cat_posts' );
function prefix_load_cat_posts () {
    $cat_id = $_POST[ 'cat' ];
         $args = array (
        'cat' => $cat_id,
        'posts_per_page' => 10,
        'order' => 'DESC'

    );

    $posts = get_posts( $args );

    ob_start ();

    foreach ( $posts as $p ) { ?>

    <div id="post-<?php echo $post->ID; ?>">
        <h1 class="posttitle"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>

        <div id="post-content">
        <?php the_excerpt(); ?>

        </div>
   </div> 


   <?php } wp_reset_postdata();

   $response = ob_get_contents();
   ob_end_clean();

   echo $response;
   die(1);
}

使用以下JS进行AJAX:

    <script>
function cat_ajax_get(catID) {
    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-2").show();
    var ajaxurl = '/wp-admin/admin-ajax.php';
    jQuery.ajax({
        type: 'POST',
        url: ajaxurl,
        data: {"action": "load-filter", cat: catID },
        success: function(response) {
            jQuery("#category-post-content").html(response);
            jQuery("#loading-animation").hide();
            return false;
        }
    });
}
</script>

我的问题是如何使用自定义分类法类别?因为我以前从未这样做过,所以不确定。任何帮助都将十分感激。谢谢。
1个回答

7
您需要使用 tax_query 替代 cat。而 category 用于原生的WordPress分类法,因此对于自定义分类法无效。
请将您的数组$args替换为以下内容:
$args = array (
    'tax_query' => array(
         array(
            'taxonomy' => 'jobs_category',
            'field' => 'term_id',
            'terms' => array( $cat_id )
         )
    ),
    'post_type' => 'jobs', // <== this was missing
    'posts_per_page' => 10,
    'order' => 'DESC'
);

谢谢你的帮助!!我想我明白你的意思了,基本上我需要寻找tax_query而不是cat。我的当前代码现在看起来像这样:http://pastebin.com/jAquKr73,但它似乎没有起作用:( - andy jones
@andyjones,你能解释一下为什么不工作吗?它还输出了什么? - Rahil Wazir
抱歉,我应该解释一下,它什么也没有加载。没有任何内容加载到:<div id="category-post-content"></div> 我使用console.log(data);来检查可能加载的内容,但是什么也没有:/ - andy jones
@andyjones 哦,我明白了。你说你有自定义文章类型,但是你没有在 $args 数组中定义任何 post_type。正如我所说的,WordPress 认为你的意思是 post_type => post,这是默认的,但不适用于你的情况。顺便问一下,你的 post_type 是什么?然后我会修改我的答案。 - Rahil Wazir
啊,好的。我把所有我的自定义文章类型放在了这个 pastebin 上:http://pastebin.com/L9KHfqvA。register_post_type( 'jobs', $args ); 非常感谢 :) - andy jones
看起来有些东西正在运作 :) 现在我只需要弄清楚如何拉取自定义字段。感谢您的所有帮助。 - andy jones

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接