Php Ajax请求只返回一个wordpress帖子,而不是整个查询
我试图创建一个“加载更多帖子”按钮,向服务器发出ajax请求。 这里的问题是,它只带来了一个帖子,而我预期有四个 在这里加载更多帖子。phpPhp Ajax请求只返回一个wordpress帖子,而不是整个查询,php,jquery,ajax,wordpress,Php,Jquery,Ajax,Wordpress,我试图创建一个“加载更多帖子”按钮,向服务器发出ajax请求。 这里的问题是,它只带来了一个帖子,而我预期有四个 在这里加载更多帖子。php <?php define('WP_USE_THEMES', false); require_once('../../../wp-load.php'); $the_query = new WP_Query( array( 'tag' => 'recommended', 'posts_per_page'=> 4 ) ); i
<?php
define('WP_USE_THEMES', false);
require_once('../../../wp-load.php');
$the_query = new WP_Query( array( 'tag' => 'recommended', 'posts_per_page'=> 4 ) );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<div class="recommended-post col-xs-12 col-sm-6 col-lg-3 ">
<a href="<?php echo get_permalink(); ?>">
<div class="rotateWrap">
<div class="imgWrap" style=" background:url('<?php the_post_thumbnail_url(); ?>'); ">
</div>
<?php foreach((get_the_category()) as $category) {
$catName = $category->cat_name;
$catSlug = $category->slug;
?>
<div class="postCategory text-center" style="background-color:<?php getColor($catSlug); ?> !important">
<?php echo $catName; ?>
</div>
</div>
<h2 class="postTitle text-center">
<?php echo get_the_title(); ?>
</h2>
</a>
</div>
<?php endwhile; endif ;?>
基本
MyScript.js
$('#ajaxCall').click( function() {
$('#newPosts').load('my_query.php')
my_query.php
<?php
define('WP_USE_THEMES', false);
require_once('../../../wp-load.php');
$the_query = new WP_Query( array( 'tag' => 'recommended', 'posts_per_page'=> 4 ) );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<div class="recommended-post col-xs-12 col-sm-6 col-lg-3 ">
<a href="<?php echo get_permalink(); ?>">
<div class="rotateWrap">
<div class="imgWrap" style=" background:url('<?php the_post_thumbnail_url(); ?>'); ">
</div>
<?php foreach((get_the_category()) as $category) {
$catName = $category->cat_name;
$catSlug = $category->slug;
?>
<div class="postCategory text-center" style="background-color:<?php getColor($catSlug); ?> !important">
<?php echo $catName; ?>
</div>
</div>
<h2 class="postTitle text-center">
<?php echo get_the_title(); ?>
</h2>
</a>
</div>
<?php endwhile; endif ;?>
使用load
而不是使用ajax
有什么原因吗?@eeya不太可能。但它确实起作用了!你现在找到解决问题的方法了吗?完全错了:)最好使用get\u posts
而不是WP\u Query,你必须使用WP\u ajax
而不是两次加载WP(WP load.php
)。。。。见示例: