在我正在工作的Wordpress网站上,它按类别列出帖子,但我也想要一个列出所有帖子的页面(带有分页,每页显示10个)。我该如何去实现这个目标呢?
谢谢
发布于 2011-01-25 23:31:49
您可以创建一个新的页面模板,其中包含以下循环:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'post', 'posts_per_page' => 10, 'paged' => $paged );
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post(); ?>
<h2><?php the_title() ?></h2>
<?php endwhile; ?>
<!-- then the pagination links -->
<?php next_posts_link( '← Older posts', $wp_query ->max_num_pages); ?>
<?php previous_posts_link( 'Newer posts →' ); ?>发布于 2014-03-13 14:22:14
对于其他可能在谷歌上搜索这篇文章的人来说...如果您已将站点的首页替换为静态页面,但仍希望帖子列表显示在单独的链接下,则需要:
现在,当你在菜单中点击这个页面的链接时,它应该会列出你最近发表的所有帖子(不需要修改代码)。
发布于 2014-02-28 00:29:43
基于@Gavins答案的更奇特的解决方案
<?php
/*
Template Name: List-all-chronological
*/
function trimStringIfTooLong($s) {
$maxLength = 60;
if (strlen($s) > $maxLength) {
echo substr($s, 0, $maxLength - 5) . ' ...';
} else {
echo $s;
}
}
?>
<ul>
<?php
$query = array( 'posts_per_page' => -1, 'order' => 'ASC' );
$wp_query = new WP_Query($query);
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php the_permalink() ?>" title="Link to <?php the_title_attribute() ?>">
<?php the_time( 'Y-m-d' ) ?>
<?php trimStringIfTooLong(get_the_title()); ?>
</a>
</li>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts published so far.'); ?></p>
<?php endif; ?>
</ul>https://stackoverflow.com/questions/4794622
复制相似问题