我试图通过抓取我设置为post-id号码的div的数据值来排除已经加载到页面上的wordpress循环中的帖子。然后我将id收集到一个数组中并通过ajax post传递给我的php文件,然后将数组传递给get_posts()函数的wordpress post__not_in。
我的ajax和循环工作正常但是当我将post__not_in设置为存储我的id数组的变量时,我的ajax不会加载任何帖子。我是一个初学者,所以试图教自己而不是使用插件。任何人都可以看到我做错了什么/我试图做到这一点的方式是什么?下面的乱码:
var postgetDump = [];
$('#test-click p').on('click', function (e) {
e.preventDefault();
$('#category-post-content').find('div').each(function() {
var getDump = parseInt($(this).data('value'), 10);
postgetDump.push(getDump);
});
console.log(postgetDump);
function posts_ajax_get(postsID) {
$("a.ajax").removeClass("current");
$("a.ajax").addClass("current"); //adds class current to the category menu item being displayed so you can style it with css
$("#loading-animation").show();
var ajaxurl = "http://localhost/pulse/pulse/wp-admin/admin-ajax.php";
console.log(ajaxurl);
$.ajax({
type: 'POST',
url: ajaxurl,
data: {
"action": "load-filter-posts",
postsid: postsID
},
success: function (response) {
$("#category-post-content").append(response);
$("#loading-animation").hide();
return false;
}
});
}
posts_ajax_get(postgetDump);
});
add_action( 'wp_ajax_nopriv_load-filter-posts', 'prefix_load_more_posts' );
add_action( 'wp_ajax_load-filter-posts', 'prefix_load_more_posts' );
function prefix_load_more_posts () {
//global $do_not_duplicate;
$posts_id = $_POST[ 'postsid' ];
$arr = implode(",", $posts_id);
$ints = array_map('intval', explode(',', $arr));
$args3 = array (
'posts_per_page' => 6,
'post__not_in' => $ints,
'offset' => 6,
'order' => 'DESC'
);
print_r($args3);
var_dump($args3);
var_export($args3);
global $post;
$posts = get_posts( $args3 );
ob_start (); ?>
<?php
foreach ( $posts as $post ) {
setup_postdata( $post ); //$do_not_duplicate[] = $post->ID;
//if (!in_array($post->ID, $do_not_duplicate)) continue; ?>
<div id="post-<?php echo $post->ID; ?>" class="col-md-4 col-sm-6" data-value="<?php echo $post->ID; ?>">
<a href="<?php echo get_permalink(); ?>"><?php the_post_thumbnail() ; ?></a>
<a href="<?php echo get_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
<p><?php the_excerpt(); ?> <span><a href="<?php echo get_permalink(); ?>"> ...Read More</a></span></p>
</div><!-- /.col-lg-4 -->
<?php } wp_reset_postdata();
$response = ob_get_contents();
ob_end_clean();
echo $response;
die(1);
}