使用ajax和自定义帖子类型为Wordpress创建loadmore?

我在使用ajax时遇到一些loadmore问题。谁能帮我。

我有代码

<button class="load-more">More</button>

功能

add_action('wp_ajax_loadmore','get_post_loadmore');
add_action('wp_ajax_nopriv_loadmore','get_post_loadmore');
function get_post_loadmore() {
    $offset = isset($_POST['offset']) ? (int)$_POST['offset'] : 0;
    $getposts = new WP_query(); $getposts->query('post_type=event&post_status=publish&showposts=5&offset='.$offset);
    global $wp_query; $wp_query->in_the_loop = true; 
    while ($getposts->have_posts()) : $getposts->the_post(); ?>
        <?php do_action('postthumb1')?>
    <?php endwhile; wp_reset_postdata();
    die(); 
}

js

<script>
    $(document).ready(function(){
        var offset = 4;
        $('.load-more1').click(function(event) {
            $.ajax({
                type : "post",dataType : "html",async: false,url : '<?php echo admin_url('admin-ajax.php');?>',data : {
                    action: "loadmore",offset: offset,},beforeSend: function(){

                },success: function(response) {
                    $('.list-new').append(response);
                    offset = offset + 5; 
                },error: function( jqXHR,textStatus,errorThrown ){
                    console.log( 'The following error occured: ' + textStatus,errorThrown );
                }
           });
        });
    });
</script>

代码可以正常运行,但是仅加载post_type事件。我想设置一个可用于所有自定义帖子类型的变量。 我尝试设置变量,但是代码不起作用。有Ajax经验的人可以帮助我吗?

nancy69669 回答:使用ajax和自定义帖子类型为Wordpress创建loadmore?

这将为您提供网站上的所有自定义帖子类型:

$args = array(
   'public'   => true,'_builtin' => false,);

$output = 'names'; // names or objects,note names is the default
$operator = 'and'; // 'and' or 'or'

$post_types = get_post_types( $args,$output,$operator ); 

$args = array(  
    'post_type' => $post_types,'post_status' => 'publish','posts_per_page' => 5,'offset' => $offset
);

$getpost = new WP_Query( $args ); 

如果您正在寻找ajax调用传递的特定post_type:


<script>
    $(document).ready(function(){
        var offset = 4;
        $('.load-more1').click(function(event) {
            $.ajax({
                type : "post",dataType : "html",async: false,url : '<?php echo admin_url('admin-ajax.php');?>',data : {
                    action: "loadmore",offset: offset,posttype: posttype //Where ever you want to get this from
                },beforeSend: function(){

                },success: function(response) {
                    $('.list-new').append(response);
                    offset = offset + 5; 
                },error: function( jqXHR,textStatus,errorThrown ){
                    console.log( 'The following error occured: ' + textStatus,errorThrown );
                }
           });
        });
    });
</script>

功能:

add_action('wp_ajax_loadmore','get_post_loadmore');
add_action('wp_ajax_nopriv_loadmore','get_post_loadmore');

function get_post_loadmore() {
    $offset = isset($_POST['offset']) ? (int)$_POST['offset'] : 0;
    $posttype = isset($_POST['posttype']) ? $_POST['posttype'] : 'post';

    $args = array(  
       'post_type' => $posttype,'offset' => $offset
    );
    $getposts = new WP_query($args); 

    global $wp_query; 
    $wp_query->in_the_loop = true; 

    while ($getposts->have_posts()) : $getposts->the_post(); 
         do_action('postthumb1');
    endwhile;

    wp_reset_postdata();
    die(); 
}
本文链接:https://www.f2er.com/3138627.html

大家都在问