首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Wordpress Ajax自定义分类法

Wordpress Ajax自定义分类法
EN

Stack Overflow用户
提问于 2014-05-30 10:24:25
回答 1查看 6.8K关注 0票数 4

我现在有一个wordpress,它有一个定制的post类型和一个自定义分类法。( jobs_category )。

我的构建列出了这个分类法中的类别,如下所示:

代码语言:javascript
复制
<?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>

然后,我在函数文件中使用了以下内容:

代码语言:javascript
复制
    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:

代码语言:javascript
复制
    <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>

我的问题是如何让它使用自定义分类类别?我不能百分之百肯定,因为我以前从来没有这样做过。

任何帮助都会很好。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-30 10:35:44

您必须使用tax_query而不是cat。虽然category用于本地Wordpress分类法,但是它不能用于自定义分类法。

将数组$args替换为:

代码语言:javascript
复制
$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'
);
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23952336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档