我可以在WordPress的ajax.php中动态设置POST类型值吗

时间:2018-12-29 作者:Subhasish Nath

    <?php
    /*

    @package subhasishlive theme

        ========================
            AJAX FUNCTIONS
        ========================
    */

    add_action( \'wp_ajax_nopriv_sunset_load_more\', \'sunset_load_more\' );
    add_action( \'wp_ajax_sunset_load_more\', \'sunset_load_more\' );
    function sunset_load_more() {

    $paged = $_POST["page"]+1;

    $query = new WP_Query( array(
        \'post_type\' => \'Allkitchenposts\',
        \'order\' => \'DESC\',
        \'post_status\'    => \'publish\',
        \'paged\' => $paged,
        \'posts_per_page\' => \'10\'
    ) );

    if( $query->have_posts() ):

        while( $query->have_posts() ): $query->the_post();

            // get_template_part( \'template-parts/content\', get_post_format() );
        ?>

              <div class="col-sm-6 single-post-news">
                <div class="single-post-content">
                  <div class="post-box reveal-block text-left">
                    <div class="post-box-img-wrap">
                        <a href="<?php the_permalink(); ?>">
                        <?php 
                        if (has_post_thumbnail( get_the_ID() ) ){
                            $image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), \'single-post-thumbnail\' );
                            ?>
                            <img src="<?php echo $image[0]; ?>" alt="" class="img-responsive"/>
                        <?php
                        }else{
                        ?>

                        <img src="<?php bloginfo(\'template_directory\'); ?>/img/news-thumb-01.jpg" class="img-responsive" />

                        <?php
                        }
                        ?>  

                        </a>
                    </div>
                    <div class="post-box-caption">
                      <div class="post-box-title text-ubold"><a href="<?php the_permalink(); ?>" class="text-gray-base"><?php the_title(); ?></a></div>
                      <ul class="list-inline post-box-meta list-inline-dashed list-inline-dashed-xs text-extra-small-10 offset-top-12 text-silver-chalice">
                        <li class="text-uppercase"><?php echo meks_time_ago(); ?></li>
                        <li class="p text-uppercase"><span>by <a href="testimonials.html"><?php the_author(); ?></a></span></li>
                      </ul>
                    </div>
                  </div>
                </div>
              </div>


        <?php
        endwhile;

    endif;

    wp_reset_postdata();

    die();

}

?>
与“post\\u type”=>“Allkitchenposts”不同,我想根据我的帖子类型动态设置此值,以便我可以在不同的归档页面中使用ajax加载内容,就像我在归档Allkitchenposts中所做的那样。php

      <div class="col-xs-12 text-center">
        <a class="btn btn-lg btn-default sunset-load-more" data-page="1" data-url="<?php echo admin_url(\'admin-ajax.php\'); ?>">Load More...</a>
      </div>
下面是我的ajax加载。js文件

jQuery(document).ready( function($){


$(document).on(\'click\',\'.sunset-load-more\', function(){

// this refers to the button itself
var that = $(this);
// reference to the data-page attribute value of the button
var page = $(this).data(\'page\');
// next page reference
var newPage = page+1;
// reference to the data-url attribute value of the button
var ajaxurl = that.data(\'url\');


$.ajax({

url : ajaxurl,
type : \'post\', // post method of retriving data
data : {

    page : page,
    action: \'sunset_load_more\'

},
error : function( response ){
    console.log(response);
},
success : function( response ){

    that.data(\'page\', newPage);
    $(\'.sunset-posts-container\').append( response );

}

});
});
});
我想知道如何通过ajax为不同的自定义帖子类型归档页面加载更多帖子????目前我只能为一个帖子类型做这件事。谢谢:)

1 个回复
最合适的回答,由SO网友:Techno Deviser 整理而成

我在single{post type}上使用了这个。

$t_slug = get_query_var(\'post_type\');

 <div class="col-xs-12 text-center">
        <a class="btn btn-lg btn-default sunset-load-more" data-page="1" post_type =<?php echo $t_slug ;?> data-url="<?php echo admin_url(\'admin-ajax.php\'); ?>">Load More...</a>
  </div>

jQuery(document).ready( function($){


$(document).on(\'click\',\'.sunset-load-more\', function(){
var post_type = $(this).attr(\'post_type\');
// this refers to the button itself
var that = $(this);
// reference to the data-page attribute value of the button
var page = $(this).data(\'page\');
// next page reference
var newPage = page+1;
// reference to the data-url attribute value of the button
var ajaxurl = that.data(\'url\');


$.ajax({

url : ajaxurl,
type : \'post\', // post method of retriving data
data : {

    post_type : post_type ,
    page : page,
    action: \'sunset_load_more\'

},
error : function( response ){
    console.log(response);
},
success : function( response ){

    that.data(\'page\', newPage);
    $(\'.sunset-posts-container\').append( response );

}

});
});
});


function sunset_load_more() {
$post_type = $_POST[\'post_type\'];
    $paged = $_POST["page"]+1;

    $query = new WP_Query( array(
        \'post_type\' => \' $post_type\',
        \'order\' => \'DESC\',
        \'post_status\'    => \'publish\',
        \'paged\' => $paged,
        \'posts_per_page\' => \'10\'
    ) );
}

相关推荐

如何在AJAX中处理400状态

我在wordpress中创建了一个页面,该页面托管在localhost中。包含两个下拉列表的页面。一个是从名为“category”的表中选择类别列表,第二个下拉列表取决于第一个,即从“subcategory”表中选择子类别。类别id将添加到每个子类别值。子类别下拉列表的值取决于类别下拉列表,我尝试使用ajax实现这一点。我的php文件test\\u ajax的代码。php jQuery(document).ready( function(){ jQuery(\'#categ