AJAX加载更多内容从x个帖子开始

时间:2016-11-08 作者:Mikelis Baltruks

我正在使用Ajax Load more 插件
我已经在点击按钮时加载了X篇文章,但在页面加载时,最初没有加载任何文章(很明显)<我应该怎么做才能自动加载(比如)10篇文章,然后只在点击按钮时加载下一篇
目前的代码是:

    echo do_shortcode(\'
      [ajax_load_more category="\'.$category->slug.\'" 
      posts_per_page="10" 
      pause="true" 
      scroll="false" 
      button_label="Load articles" 
      button_loading_label="Loading..."]\'
    );
我是否必须自己编写该功能,或者有某种方法可以通过更改短代码来实现?!

2 个回复
最合适的回答,由SO网友:Benoti 整理而成

您需要设置offset 在查询中。

要获得正确的偏移值,需要存储偏移值并将其发送回ajax脚本(data-offset 在输入或隐藏字段中)。

$args = array(
          \'posts_per_page\'=> 10,
          \'post_status\'=> \'publish\',
          \'offset\'=> $_POST[\'offset\']
);
$_POST[\'offset\'] 来自您的按钮和js脚本。

SO网友:kundan prasad

https://youtu.be/6U92B_MHfqU

复制并粘贴此视频注释中的尺寸代码。这是在wordpress中创建自定义ajax加载的最简单方法。按照步骤进行操作,最后生成自定义ajax加载更多。

step1:职位类型代码:

//start video post
$labels2 = array(
    \'name\' => _x(\'video\', \'post type general name\', \'robin\'),
    \'singular_name\' => _x(\'video\', \'post type singular name\', \'robin\'),
    \'add_new\' => _x(\'Add New\', \'video\', \'robin\'),
    \'add_new_item\' => __(\'Add New video\', \'robin\'),
    \'edit_item\' => __(\'Edit video\', \'robin\'),
    \'new_item\' => __(\'New video\', \'robin\'),
    \'all_items\' => __(\'All video\', \'robin\'),
    \'view_item\' => __(\'View video\', \'robin\'),
    \'search_items\' => __(\'Search video\', \'robin\'),
    \'not_found\' =>  __(\'No video found\', \'robin\'),
    \'not_found_in_trash\' => __(\'No video found in Trash\', \'robin\'),
    \'parent_item_colon\' => \'\',
    \'menu_name\' => \'video\'
);
$args2 = array(
    \'labels\' => $labels2,
    \'public\' => true,
    \'publicly_queryable\' => true,
    \'show_ui\' => true,
    \'show_in_menu\' => true,
    \'query_var\' => true,
    \'rewrite\' => true,
    \'capability_type\' => \'post\',
    \'has_archive\' => true,
    \'hierarchical\' => false,
    \'menu_position\' => null,
    \'taxonomies\'    => array( \'category\',\'post_tag\'),
    \'supports\' => array(\'title\',\'editor\',\'thumbnail\')
);
register_post_type(\'video\',$args2);
//end video post
step2:功能挂钩:

//load more  ajax
/* ===============================================*/
function my_wfsubmit_callback() {
    $id = $_POST[\'hide\'];
    $current=$id;
    $prev=2;
    $prev=$prev+$current;
    $i=0;
    $the_query = new WP_Query(array( \'post_type\' => \'video\', \'posts_per_page\' =>\'-1\',\'order\'=>\'ASC\',\'orderby\'=>\'date\' ));
    if($the_query->have_posts()):
        while($the_query->have_posts()):$the_query->the_post();?>
            <?php if($i>=$current && $i<$prev){?>

                <img alt="" src="<?php
                $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($query->ID),\'full\');
                echo $thumb[0];?>">
                <?php the_title();?>
                <?php the_content();?>
            <?php }$i++;?>
        <?php endwhile;
        wp_reset_postdata();
    endif;
    die();
}
add_action( \'wp_ajax_wfsubmit_action\', \'my_wfsubmit_callback\' ); //wfsubmit_action = ajax action with wordpress hook wp_ajax_wfsubmit_action
add_action( \'wp_ajax_nopriv_wfsubmit_action\', \'my_wfsubmit_callback\' ); //wfsubmit_action = ajax action with wordpress hook wp_ajax_nopriv_wfsubmit_action
/* ================= end ajax submit ===============*/
step3:PHP代码:

<?php $args = array( \'post_type\' => \'video\', \'posts_per_page\' =>\'4\',\'order\'=>\'ASC\',\'orderby\'=>\'date\' );
$query=new WP_Query($args);

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

    <img alt="" src="<?php
    $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($query->ID),\'full\');
    echo $thumb[0];?>">
    <?php the_title();?>
<?php endwhile;?>
<div id="sendtxt"></div>

<?php $args1 = array( \'post_type\' => \'video\', \'posts_per_page\' =>\'-1\',\'order\'=>\'ASC\',\'orderby\'=>\'date\' );
$query1=new WP_Query($args1);
$c=0;
while ( $query1->have_posts() ) : $query1->the_post();
    $c++;
endwhile;
wp_reset_postdata();
$max=$c;
?>

<form id="webform" role="form" class="webform" >
      <textarea name="hide" style="display:none;" class="CUR"  id="output">4
      </textarea>
    <a href="javascript:void(0)" id="webformsubmit" class="read-btn blck">Load More</a>
</form>
step4:脚本代码:

<script type="text/javascript" src="/wordpress/js/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
    $(\'#webformsubmit\').click(function(event){
        event.preventDefault();
        var priBox = "extra";   //extra data,field,tag
        var ajaxurlsb = "<?php echo admin_url( \'admin-ajax.php\' );?>"

        $.ajax({
            type: "POST",
            url: ajaxurlsb+\'?action=wfsubmit_action\',
            data: $("#webform").serialize()+\'&txtp=\'+priBox,
            success: function(data)    { // Get the result and asign to each cases
//$(\'#sendtxt\').html(data);  //output div ID (sendtxt)
                $(\'#sendtxt\').append(data);
            }
        });

    });
    // counter
    $(\'#webformsubmit\').click(function() {
        $(\'#output\').html(function(i, val) { return val*1+2 });
    });

    // button display


    $(\'#webformsubmit\').click(function() {
        var curr_val = $(\'#output\').val();
        if(curr_val ><?php echo $max;?>){
            $("#webformsubmit").css("display", "none");
        }
    });

</script>
<script type="text/javascript" src="/wordpress/js/jquery-3.1.1.min.js"></script>
step5: jquery下载链接:

https://jquery.com/download/

相关推荐

尝试在WordPress中实现AJAX注释,遇到WP错误

我试图在WordPress中为我的评论实现Ajax,使用this tutorial. 但我在将教程中的代码集成到自己的预构建主题时遇到了问题。问题是,我要么得到一个WP错误“检测到重复注释;看来你已经说过了!”或标准500错误。以下是我得到的:下面是我对ajax的评论。js文件如下所示: * Let\'s begin with validation functions */ jQuery.extend(jQuery.fn, { /* * check i