【问题标题】:Wordpress Pagination not working, it would not move to the next pageWordpress 分页不起作用,它不会移动到下一页
【发布时间】:2018-07-20 05:44:32
【问题描述】:

目前,我正在使用 wp_query 在博客页面上显示我的文章。我遇到了分页问题。在我的 wp_query 中实现分页代码后(请参见下面的代码),分页不起作用,每次我按第 2 页或下一页时,它仍然显示第一页。此外,当我悬停分页编号和下一步按钮时,它显示链接都是相同的。

这是我的 php 代码。

<?php 

$args = array('post_status' => 'publish');
$wp_query = new WP_Query($args);
$big = 99999999999999;
if($wp_query->have_posts()) :
 while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
    <a href="<?php echo the_permalink() ?>"><h2><?php the_title(); ?></h2></a>
    <p><?php the_post_thumbnail( 'large', array( 'class' => 'img-responsive' )); ?></p>         
    <p><?php the_excerpt(); ?></p>
<?php endwhile; ?>

<?php echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $wp_query->max_num_pages
) );
?>
<?php endif; ?>

希望你能告诉我我的问题。提前致谢。

【问题讨论】:

  • 首先请更改您的变量$wp_query,因为它的默认wordpress变量可能是这个变量导致的问题
  • 运气不好。我更改了所有 $wp_query 变量,但仍然无法正常工作。单击第 2 页后,它仍然显示第一页文章
  • 请在此$args = array('post_status' =&gt; 'publish'); 中添加'paged' =&gt; $paged 喜欢$args = array('post_status' =&gt; 'publish','paged' =&gt; $paged);
  • 我刚刚在 $args 数组下添加了 'paged' => $paged。但是我点击页面2后出现问题页面中的所有文章/内容都不见了
  • 请在您的$args 数组上方添加$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

标签: php wordpress pagination


【解决方案1】:

此代码对我有用。试试这个:

<?php 
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array('post_status' => 'publish','paged' => $paged);
$wp_query = new WP_Query($args);
$big = 99999999999999;
if($wp_query->have_posts()) :
 while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
    <a href="<?php echo the_permalink() ?>"><h2><?php the_title(); ?></h2></a>
    <p><?php the_post_thumbnail( 'large', array( 'class' => 'img-responsive' )); ?></p>         
    <p><?php the_excerpt(); ?></p>
<?php endwhile; ?>
<?php echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $wp_query->max_num_pages
) );
?>
<?php endif; ?>

【讨论】:

  • 运气不好。我在自定义页面上实现了您的代码,但是在单击第 2 页后,所有文章/内容都消失了 :( 我在我的文章中使用 Wordpress 默认帖子,并使用 WP_Query 在我的自定义博客页面中显示该帖子。跨度>
  • 您的网站是在本地还是在服务器上??你能发一个网站链接吗?然后我会检查最大页数。
  • 抱歉,因为我正在本地服务器上开发它,所以目前无法向您发送链接
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-25
  • 2018-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多