使用AJAX自动加载子类别内容

时间:2016-02-25 作者:Danc Chan

最近,我在这里发布了一个关于使用AJAX部署子类别内容的问题。多亏了ccprog,它工作得很好。然而,在我与我的客户分享之后,又出现了另一个问题。他们希望在我单击下一个子类别之前,第一个子类别的内容立即显示在页面上。为了让事情更清楚,下面是一个例子:

Cat A | B类| C类

sub 1 | 子2 |子3

单击Cat A时,sub 1内容应立即显示在页面上,然后再单击sub 2/sub 3(ajax内容)

我确实试着绕过这些代码,但一点运气都没有。

下面是我正在使用的JS代码

  var links = $(".filter a.ajax");
  var ajaxurl = \'<?php echo admin_url(\'admin-ajax.php\'); ?>\';

   function cat_ajax_get (event) {
     var catIDText = $(event.target).parent().prop(\'id\');
     var catID = catIDText.substring(4);

     links.removeClass("selected"); // remove class from all lines
     $(event.target).addClass("selected"); // only add it back on the selected

    $.ajax({
        type: \'POST\',
        url: ajaxurl,
        data: {"action": "load-filter", cat: catID },
        success: function(response) {
            $(".category-post-content").html(response);
            return false;
        }
    });
  }
  links.click(cat_ajax_get);
以下是页面中的代码

  <?php //Best Practices ?>
    <div class="tabs__content" id="best-practices">
        <nav class="category-group">
            <?php $categories = get_categories(array(\'child_of\' => 4));
                ?>
                <ul class="filter">
                    <?php foreach($categories as $cat) { ?>
                        <li id="cat-<?php echo $cat->term_id; ?>"><a class="<?php echo $cat->slug; ?> ajax" href="#"><?php echo $cat->name; ?></a></li>
            <?php } ?>
                </ul>
        </nav>

        <div class="category-post-content"></div>
    </div>
  <?php //End Best Practices ?>
这是函数文件中的代码

 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
   );

   $posts = get_posts( $args );
            global $post;

 ob_start ();

 foreach ( $posts as $post ) {
  setup_postdata( $post ); ?>

    <div id="post-<?php echo $post->ID; ?>" <?php post_class(); ?>>
      <div class="article">
        <div class="article__thumbnail"><?php the_post_thumbnail(); 
   ?></div>
        <div class="article__content">
            <div class="article__header"><?php the_title(); ?></div>
            <div class="article__brief"><?php 
     the_field(\'post_excerpt\'); ?></div>
             <div class="article__link"><?php the_content(); ?></div>
  </div>
    </div>
</div>

<?php } wp_reset_postdata();

 $response = ob_get_contents();
 ob_end_clean();

 echo $response;
 die(1);
 }
谁能告诉我一些方向吗?非常感谢。

1 个回复
SO网友:majick

这里有一种方法。。。将AJAX函数拆分为:

 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\' ];
   echo load_cat_posts($cat_id);
   die(1);
 }

 function load_cat_posts ($cat_id) {

   $args = array (
    \'cat\' => $cat_id,
    \'posts_per_page\' => 10
   );

   $posts = get_posts( $args );
        global $post;

 ob_start ();

 foreach ( $posts as $post ) {
  setup_postdata( $post ); ?>

    <div id="post-<?php echo $post->ID; ?>" <?php post_class(); ?>>
      <div class="article">
    <div class="article__thumbnail"><?php the_post_thumbnail(); 
   ?></div>
    <div class="article__content">
        <div class="article__header"><?php the_title(); ?></div>
        <div class="article__brief"><?php 
     the_field(\'post_excerpt\'); ?></div>
         <div class="article__link"><?php the_content(); ?></div>
  </div>
    </div>
</div>

<?php } wp_reset_postdata();

 $response = ob_get_contents();
 ob_end_clean();

 return $response;
}
在模板中,添加一行以获取循环中的第一个子类别,并直接调用split函数:

<?php //Best Practices ?>
  <div class="tabs__content" id="best-practices">
    <nav class="category-group">
        <?php $categories = get_categories(array(\'child_of\' => 4));
            ?>
            <ul class="filter">
                <?php $firstsubcat = \'\'; // clear just in case 
                    foreach($categories as $cat) { 
                    // to grab the first subcategory
                    if ($firstsubcat == \'\') {$firstsubcat = $cat->term_id;} ?>
                    <li id="cat-<?php echo $cat->term_id; ?>"><a class="<?php echo $cat->slug; ?> ajax" href="#"><?php echo $cat->name; ?></a></li>
        <?php } ?>
            </ul>
    </nav>

    // call the function directly
    <div class="category-post-content"><?php echo load_cat_posts($firstsubcat); ?></div>
  </div>
<?php //End Best Practices ?>
或者,您可以使用jQuery在文档加载时以正确的ID为目标并假装单击以调用AJAX(将“73”替换为子类别ID):

add_action(\'wp_footer\',\'subcat_loader\');
function subcat_loader() {
    echo "<script>jQuery(document).ready(function($) {$(\'#cat-73\').click();});</script>";
} 

相关推荐

尝试在WordPress中实现AJAX注释,遇到WP错误

我试图在WordPress中为我的评论实现Ajax,使用this tutorial. 但我在将教程中的代码集成到自己的预构建主题时遇到了问题。问题是,我要么得到一个WP错误“检测到重复注释;看来你已经说过了!”或标准500错误。以下是我得到的:下面是我对ajax的评论。js文件如下所示: * Let\'s begin with validation functions */ jQuery.extend(jQuery.fn, { /* * check i