我正在开发一个基于单页应用程序(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>
最合适的回答,由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.phpwp_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