【问题标题】:adding a load more posts button添加加载更多帖子按钮
【发布时间】:2017-06-24 20:49:53
【问题描述】:

我想在我的 wordpress 网站的首页添加一个简单的加载更多按钮,以加载更多帖子。我正在使用查询和引导程序,每行有 1 个帖子,然后是 2 个,然后是 1 个,依此类推。我希望显示 15 个帖子,然后显示一个加载更多按钮,然后当您按下按钮时,将加载 15 个更多帖子。我已经开始尝试添加这个按钮,但是每次尝试都不起作用。我已经尝试过插件并制作自己的插件。如果有人可以帮助我添加加载更多按钮,我将非常感激。

我的首页.php

<?php 
/* 
* Template Name: 
*/ 

get_header(); 
get_template_part ('inc/carousel'); 

$the_query = new WP_Query( [ 
'posts_per_page' => 15, 
'paged' => get_query_var('paged', 1) 
] ); 

if ( $the_query->have_posts() ) { ?> 
<div id="ajax"> 
<?php 
$i = 0; 
$j = 0; 
while ( $the_query->have_posts() ) { $the_query->the_post(); 

if ( $i % 5 === 0 ) { // Large post: on the first iteration and every 7th post after... ?> 
<div class="row"> 
<article <?php post_class( 'col-sm-12 col-md-12' ); ?>> 
<div class="large-front-container"> 
<?php the_post_thumbnail('full', array('class' => 'large-front-thumbnail')); ?> 
</div> 
<div class="front-page-date"><?php echo str_replace('mins', 'minutes', human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'); ?></div>
<h2><a class="front-page-post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> 
<p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p> 
<div class="front-page-post-info"> 
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a> 
<?php get_template_part( 'front-shop-the-post' ); ?> 
<?php get_template_part( 'share-buttons' ); ?> 
<div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div> 
</div> 
</article> 
</div> 

<?php 

} else { // Small posts ?> 
<?php if($j % 2 === 0) echo '<div class="row">'; ?> 
<article <?php post_class( 'col-sm-6 col-md-6' ); ?>> 
<?php the_post_thumbnail('full', array('class' => 'medium-front-thumbnail')); ?> 
<div class="front-page-date"><?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?></div>
<h2><a class="front-page-post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> 
<p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p> 
<div class="front-page-post-info"> 
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
<?php get_template_part( 'front-shop-the-post' ); ?>
<?php get_template_part( 'share-buttons' ); ?>
    <div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div> 
    </div>

</article> 

<?php $j++; if($j % 2 === 0) echo '</div>'; ?> 
<?php 
} 
$i++; 
}?> 

</div> 
<?php if(get_query_var('paged') < $the_query->max_num_pages) { 
} 
} 
elseif (!get_query_var('paged') || get_query_var('paged') == '1') { 
echo '<p>Sorry, no posts matched your criteria.</p>'; 
} 
get_footer();

多米尼克的回答编辑

首页.php

<?php 
/* 
* Template Name: 
*/ 

get_header(); 
get_template_part ('inc/carousel'); 

$the_query = new WP_Query( [ 
'posts_per_page' => 15, 
'paged' => get_query_var('paged', 1) 
] ); 

if ( $the_query->have_posts() ) { ?> 
<div id="ajax"> 
<?php 
$i = 0; 
$j = 0; 
while ( $the_query->have_posts() ) { $the_query->the_post(); 

if ( $i % 5 === 0 ) { // Large post: on the first iteration and every 7th post after... ?> 
<div class="row"> 
<article <?php post_class( 'col-sm-12 col-md-12' ); ?>> 
<div class="large-front-container"> 
<?php the_post_thumbnail('full', array('class' => 'large-front-thumbnail')); ?> 
</div> 
<div class="front-page-date"><?php echo str_replace('mins', 'minutes', human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'); ?></div>
<h2><a class="front-page-post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> 
<p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p> 
<div class="front-page-post-info"> 
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a> 
<?php get_template_part( 'front-shop-the-post' ); ?> 
<?php get_template_part( 'share-buttons' ); ?> 
<div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div> 
</div> 
</article> 
</div> 

<?php 

} else { // Small posts ?> 
<?php if($j % 2 === 0) echo '<div class="row">'; ?> 
<article <?php post_class( 'col-sm-6 col-md-6' ); ?>> 
<?php the_post_thumbnail('full', array('class' => 'medium-front-thumbnail')); ?> 
<div class="front-page-date"><?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?></div>
<h2><a class="front-page-post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> 
<p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p> 
<div class="front-page-post-info"> 
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
<?php get_template_part( 'front-shop-the-post' ); ?>
<?php get_template_part( 'share-buttons' ); ?>
    <div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div> 
    </div>

</article> 

<?php $j++; if($j % 2 === 0) echo '</div>'; ?> 
<?php 
} 
$i++; 
}?> 

</div> 
<?php if(get_query_var('paged') < $the_query->max_num_pages) { ?>
    <button id=load-more>load more</button>
<?php
} 
} 
elseif (!get_query_var('paged') || get_query_var('paged') == '1') { 
echo '<p>Sorry, no posts matched your criteria.</p>'; 
} 
get_footer();

循环.php

<?php if ( $the_query->have_posts() ) {

        while ( $the_query->have_posts() ) {
          $the_query->the_post();
<?php 
$i = 0; 
$j = 0; 
while ( $the_query->have_posts() ) { $the_query->the_post(); 

if ( $i % 5 === 0 ) { // Large post: on the first iteration and every 7th post after... ?> 
<div class="row"> 
<article <?php post_class( 'col-sm-12 col-md-12' ); ?>> 
<div class="large-front-container"> 
<?php the_post_thumbnail('full', array('class' => 'large-front-thumbnail')); ?> 
</div> 
<div class="front-page-date"><?php echo str_replace('mins', 'minutes', human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'); ?></div>
<h2><a class="front-page-post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> 
<p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p> 
<div class="front-page-post-info"> 
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a> 
<?php get_template_part( 'front-shop-the-post' ); ?> 
<?php get_template_part( 'share-buttons' ); ?> 
<div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div> 
</div> 
</article> 
</div> 

<?php 

} else { // Small posts ?> 
<?php if($j % 2 === 0) echo '<div class="row">'; ?> 
<article <?php post_class( 'col-sm-6 col-md-6' ); ?>> 
<?php the_post_thumbnail('full', array('class' => 'medium-front-thumbnail')); ?> 
<div class="front-page-date"><?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?></div>
<h2><a class="front-page-post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> 
<p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p> 
<div class="front-page-post-info"> 
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
<?php get_template_part( 'front-shop-the-post' ); ?>
<?php get_template_part( 'share-buttons' ); ?>
    <div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div> 
    </div>

</article> 

<?php $j++; if($j % 2 === 0) echo '</div>'; ?> 
<?php 
} 
$i++; 
}?>  
        }
?>

functions.php

//LOAD MORE
   add_action( 'wp_ajax_load_more', 'load_more' );

  function load_more() {
    if ( isset($_POST['action']) && $_POST['action'] === 'load_more' ){
      $loopFile        = $_POST['loop_file'];
      $paged           = $_POST['page_no'];
      $posts_per_page  = $_POST['posts_per_page'];

      $args = array('paged' => $paged,
                    'post_type' => 'post',
                    'posts_per_page' => $posts_per_page);

      $query = new WP_Query($args);

      get_template_part( $loopFile );
    }

    wp_reset_query();

    exit;
   }

js

 // AJAX to grab more posts, wrap with a vanilla javascript or
  // jQuery click event function.
  function loadMorePosts(pageNumber, postsPerPage) {
    var query = 'action=load_more&page_no=' + pageNumber + 
        '&loop_file=loop&posts_per_page=' + postsPerPage;

    jQuery.ajax({
        url: '/wp-admin/admin-ajax.php',
        type: 'post',
        data: query,
        success: function(response){
            // handle response
        }
    });
}

【问题讨论】:

  • 你试过用谷歌搜索这个吗?有很多不同版本的分页示例。

标签: javascript jquery ajax button infinite-scroll


【解决方案1】:

您可以采取的一种方法是将您的查询移动到functions.php 并将一个操作添加到wp_ajax。例如:

  add_action( 'wp_ajax_load_more', 'load_more' );

  function load_more() {
    if ( isset($_POST['action']) && $_POST['action'] === 'load_more' ){
      $loopFile        = $_POST['loop_file'];
      $paged           = $_POST['page_no'];
      $posts_per_page  = $_POST['posts_per_page'];

      $args = array('paged' => $paged,
                    'post_type' => 'post',
                    'posts_per_page' => $posts_per_page);

      $query = new WP_Query($args);

      get_template_part( $loopFile );
    }

    wp_reset_query();

    exit;
   }

创建一个位于主题根目录中的 loop.php。这将是一个可以迭代的帖子模板:

<?php if ( $query->have_posts() ) {

        while ( $query->have_posts() ) {
          $query->the_post();
            // populate your post template here
            echo   '<h2>' . get_the_title() .'</h2>';    
        }
?>

将此代码放在一个 javascript 文件中。当您需要获取更多帖子时,请对操作“load_more”进行 AJAX 调用:

  // AJAX to grab more posts, wrap with a vanilla javascript or
  // jQuery click event function.
  function loadMorePosts(pageNumber, postsPerPage) {
    var query = 'action=load_more&page_no=' + pageNumber + 
        '&loop_file=loop&posts_per_page=' + postsPerPage;

    $.ajax({
        url: '/wp-admin/admin-ajax.php',
        type: 'post',
        data: query,
        success: function(response){
            // handle response
        }
    });
}

这是一个非常简单的植入,您需要包含一个 NONCE 以确保安全并在达到最大页面数时进行处理。

【讨论】:

  • 非常感谢您尝试帮助多米尼克。我已经添加了上面建议的代码,我的网站现在将无法加载。我将我的新代码添加到我的问题中。我做错了什么?
  • 我应该更清楚对不起,javascript 代码(第三部分)必须在 .js 文件中。由于 PHP 错误,我确定您现在会出现白屏。请务必在 wp_config.php 中设置 wp_debug: true 以查看错误。我在loop.php中也有一个错字(我已经编辑了我的答案。
  • 我还建议有一个基本的 loop.php 文件开始,然后在其上构建。这样您就可以更轻松地诊断问题。
  • 我的网站又可以正常工作了,谢谢。但是,仍然没有加载更多按钮。我认为这段代码从未添加到任何地方的按钮中,我会使用什么代码来做到这一点?
  • 没错,我把那部分留给了你。按钮很简单:&lt;button id=load-more&gt;load more&lt;/button&gt; 在你的 javascript 文件中监听点击事件并调用函数:$('#load-more').click( function() { loadMorePosts(2, 15); }); 我希望这会有所帮助!如果确实如此,请支持我的答案或将其标记为已接受的答案。谢谢!
猜你喜欢
  • 2017-12-21
  • 2015-10-13
  • 1970-01-01
  • 2023-03-23
  • 2021-05-18
  • 2016-06-08
  • 1970-01-01
  • 2016-07-16
  • 2018-03-26
相关资源
最近更新 更多