WordPress在点击时与多个加载更多帖子功能冲突

时间:2017-04-04 作者:Umair Razzaq

我正在开发一个基于单页应用程序(SAP)的主题Portfolio CPT在循环中显示组合项目,而不是博客文章。

我已经建立了两个单独的帖子加载更多的功能,点击就会触发。因此,博客帖子工作正常,但单击“加载更多”按钮Portfolio 它为博客而不是公文包添加帖子。

下面是博客的代码函数。

blog-functions.php

// Enqueing Scripts for loading blogs posts via Ajax
wp_enqueue_script(\'ajax_blog_scripts\', get_template_directory_uri() . \'/assets/js/blog-scripts.js\', array(), false, true );
wp_localize_script(\'ajax_blog_scripts\', \'ajaxurl\', admin_url(\'admin-ajax.php\') );

// Initiating Ajax Load More Posts Function
function loadmore_blog_posts() {

    $count = $_POST["count"];
    $cpt = 1;
    $args = array(
        \'posts_per_page\' => -1,
        \'post_type\'   => \'post\', // change the post type if you use a custom post type
        \'post_status\' => \'publish\',
    );

    $blog_posts = new WP_Query( $args );

    if( $blog_posts->have_posts() ) {
        while( $blog_posts->have_posts() ) {
            $blog_posts->the_post();

            if( $cpt > $count && $cpt < $count+3 ) {

                <article id="post-<?php the_ID(); ?>" <?php post_class(\'post-item\'); ?>>
                    Post Content...
                </article>

            <?php }
            $cpt++;
        }
    }

    die();
}
add_action( \'wp_ajax_load_more_posts\', \'loadmore_blog_posts\' );
add_action( \'wp_ajax_nopriv_load_more_posts\', \'loadmore_blog_posts\' );

blog-scripts.js

// Load Next Posts via AJAX Request
$(\'#blog-section .load-more-btn\').click(function() {

    // Adding Loading Posts spinner on click
    var $btn = $(this).button(\'loading\');

    // Sending Ajax Request for getting more Posts
    $.post(ajaxurl, {

        \'action\': \'load_more_posts\',
        \'count\': $(\'article.post-item\').length

    }, function(response) {

        $(\'#blog-section .blog-posts\').append(response);

        $btn.button(\'reset\');

    });

});

Loop Markup

<div class="blog-posts">

    <?php
    $args = array( \'order\' => $blog_post_order, \'posts_per_page\' => $blog_post_count );
    $the_query = new WP_Query( $args ); 

    if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
    ?>

    <article id="post-<?php the_ID(); ?>" <?php post_class(\'post-item col-sm-6\'); ?>>
        Post Content...
    </article>

    <?php wp_reset_postdata(); ?>
    <?php endwhile; else :  ?>
    <p><?php _e( \'Sorry, there is no post added so far. Please go to Dashboard and add one.\', \'theme-slug\' ); ?></p>
    <?php endif; ?>

</div>

下面是公文包的代码函数。

portfolio-scripts.php

// Enqueing Scripts for loading portfolio posts via Ajax
wp_enqueue_script(\'ajax_portfolio_scripts\', get_template_directory_uri() . \'/assets/js/portfolio-scripts.js\', array(), false, true );
wp_localize_script(\'ajax_portfolio_scripts\', \'ajaxurl\', admin_url(\'admin-ajax.php\') );


// Initiating Ajax Load More Posts Function
function loadmore_portfolio_posts() {

    $count = $_POST["count"];
    $cpt = 1;
    $args = array(
        \'posts_per_page\' => -1,
        \'post_type\'   => \'portfolio\', // change the post type if you use a custom post type
        \'post_status\' => \'publish\',
    );

    $portfolio_posts = new WP_Query( $args );

    if( $portfolio_posts->have_posts() ) {
        while( $portfolio_posts->have_posts() ) {
            $portfolio_posts->the_post();

            if( $cpt > $count && $cpt < $count+5 ) { ?>

                <div id="post-<?php the_ID(); ?>" <?php post_class(\'col-xl-2 col-lg-3 col-md-3 col-sm-4 col-xs-6 portfolio-item\'); ?>>
                    Loop Content
                </div>

            <?php }

            $cpt++;
        }
    }

    die();
}
add_action( \'wp_ajax_load_more_posts\', \'loadmore_portfolio_posts\' );
add_action( \'wp_ajax_nopriv_load_more_posts\', \'loadmore_portfolio_posts\' );
<div class="blog-posts">

    <?php
    $args = array( \'order\' => $blog_post_order, \'posts_per_page\' => $blog_post_count );
    $the_query = new WP_Query( $args ); 

    if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
    ?>

    <article id="post-<?php the_ID(); ?>" <?php post_class(\'post-item col-sm-6\'); ?>>
        Post Content...
    </article>

    <?php wp_reset_postdata(); ?>
    <?php endwhile; else :  ?>
    <p><?php _e( \'Sorry, there is no post added so far. Please go to Dashboard and add one.\', \'theme-slug\' ); ?></p>
    <?php endif; ?>

</div>

Loop Markup

<div class="portfolio-items">
    <?php
    $args = array( \'post_type\' => \'portfolio\', \'order\' => $portfolio_post_order, \'posts_per_page\' => $portfolio_post_count );
    $the_query = new WP_Query( $args ); 

    if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <div id="post-<?php the_ID(); ?>" <?php post_class(\'col-xl-2 col-lg-3 col-md-3 col-sm-4 col-xs-6 portfolio-item\'); ?>>
        Loop Content
    </div>

    <?php wp_reset_postdata(); ?>
    <?php endwhile; else :  ?>
    <p><?php _e( \'Sorry, there is no post added so far. Please go to Dashboard and add one.\', \'theme-slug\' ); ?></p>
    <?php endif; ?>
</div>

3 个回复
最合适的回答,由SO网友:Umair Razzaq 整理而成

谢谢你的帮助,伙计;经过一点搜索,我终于完成了这项任务。据我所知,问题是wp_localize_script. 在两者上blog-functions.php &;portfolio-functions.php.

问题在于$object_name, 所以我在ajaxurl. 下面是我的更新代码。

blog-functions.php

wp_localize_script(\'ajax_blog_scripts\', \'postAjax\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' ) ) );
portfolio-functions.php

wp_localize_script(\'ajax_portfolio_scripts\', \'portfolioAjax\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' ) ) );

每个Ajax$Object\\u名称的jQuery变量

blog-scripts.js - postAjax.ajaxurl

portfolio-scripts.js - portfolioAjax.ajaxurl

SO网友:Faysal Mahamud

检查您的投资组合脚本。js和博客脚本。js两个动作包含相同的动作。

\'操作\':\'加载\\u更多\\u帖子\',

所以对于你的投资组合脚本。js操作应为

“操作”:“loadmore\\u portfolio\\u posts”,

而不是

\'操作\':\'加载\\u更多\\u帖子\',

您可以在公文包脚本中声明函数。php,但不调用ajax。我想改变行动可以解决你的问题。

SO网友:scott

使用AJAX,您需要echo 您的返回值,就在调用之前die(). 因此

  if( $cpt > $count && $cpt < $count+3 ) {

            <article id="post-<?php the_ID(); ?>" <?php post_class(\'post-item\'); ?>>
                Post Content...
            </article>

        <?php }
尝试将输出存储在变量中,如下所示:

$content = \'\';
if( $cpt > $count && $cpt < $count+3 ) {
   $content .= "<article id=post-" . the_ID(); . post_class(\'post-item\'); . "Post Content... </article>";
}
然后在函数结束时,将其发送回AJAX调用函数:

echo $content;
wp_die();

相关推荐

JqueryUi对话框给出未捕获的TypeError:This._addClass不是函数错误

我有一个网站,我们需要一些自定义php编码来连接到外部数据库,以获取几个销售我们产品的供应商的产品评论URL。我们试图实现的基本想法是让用户注册他们的产品,然后如果他们愿意留下评论,就延长保修期。我正在使用XYZScript。com的“插入PHP”插件来实现这一点。该主题最初只加载了jQuery,以避免创建子主题,我们正在php脚本中加载jQueryUI。因此,我们将jQuery加载到文档的标题中,将jQueryUI加载到文档的正文中。我不太确定这是因为加载脚本的顺序造成的,还是其他一些冲突的javasc