php - Wordpress home.php 中的分页
问题描述
分页在 Wordpress 博客中不起作用。我已经跟踪到 home.php 的具体负责文件。
原始博客页面可以正常显示帖子(一次 10 个),但是如上所述分页不起作用。
<?php
/**
Category Page
*/
get_header(); ?>
<div class="container-fluid blogs post-section" id="blogs">
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-9 col-lg-8">
<?php echo category_description( $category_id ); ?>
<div class="category-posts">
<?php
$args = array(
"post_type" => "post",
"post_status" => "publish",
"posts_per_page" => "10",
"orderby" => "date",
"order" => "DESC"
);
$query = new WP_Query($args);
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
<div class="col-sm-12">
<div class="inner-post">
<?php if ( has_post_thumbnail() ) { ?><div class="thumbnail col-sm-4" style="background:none!important;"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a></div><?php } ?>
<div class="abso custom_abso col-sm-8"><div class="inner-box">
<div class="date">
<?php
$archive_year = get_the_time('Y');
$archive_month = get_the_time('M');
$archive_day = get_the_time('d');
?>
<span class="month"><?php echo $archive_month; ?></span> <span class="day"><?php echo $archive_day; ?>,</span><span class="year"><?php echo $archive_year; ?></span>
</div>
<h2 style="margin-top:0;"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
<p><a href="<?php the_permalink(); ?>" class="readmores">Read More <i class="fa fa-caret-right" aria-hidden="true"></i>
</a></p>
</div>
<?php endwhile; endif;
?></div>
<div class="pagination text-center">
<?php the_posts_pagination( array( 'mid_size' => 2 ) ); ?>
</div>
<?php wp_reset_query(); ?>
</div>
<?php get_sidebar(); ?>
</div></div></div>
<?php get_footer(); ?>
基于这个 Wordpress 文档,我更新了文件的顶部,如下所示:
<div class="category-posts">
<?php $args = array(
'posts_per_page' => 10,
'offset' => 0,
'cat' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true,
'fields' => '',
);
$posts_array = get_posts( $args ); ?>
<div class="col-sm-12">
这很有效,因为分页按钮可以工作,但是,每页只显示一篇文章:
关于如何每页显示 10 个帖子的任何建议?
解决方案
请添加以下参数“分页”。它设置分页查询变量。分页是根据这个参数工作的。
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
到$args
.
推荐阅读
- postgresql - 并发数据插入客户端(golang)导致数据库(postgres)中缺少前 50 行,但其余 390 行还可以
- python - 如何在 python 脚本中格式化 postgreSQL 查询以获得更好的可读性?
- android - 在 MPAndroid 条形图中将 x 轴位置设置为 y = 0
- python - 子字符串中的模式限制 - Python
- python - 深度优先搜索的意外结果
- reactjs - 反应:每次启动时都没有定义我的状态变量
- ruby - 有条件地在 RSpec 循环中应用标签
- eclipse - 如何在 application.yml 中为 @ConfigurationProperties 声明的自定义属性启用 Eclipse 自动完成?
- php - 如果为空,不更改文件上传的数据库值?
- java - +-*/ 有理数上的 Junit.framework.AssertionFailedError,但预期值和结果相同