Wordpress - 列出所有帖子(使用proper_pagination)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4794622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:42:41  来源:igfitidea点击:

Wordpress - List all posts (with proper_pagination)

wordpress

提问by Probocop

On the Wordpress site I'm working on, it lists posts by category, but I am also after a page that lists ALL the posts (with pagination, showing 10 per page). How would I go about achieving this?

在我正在处理的 Wordpress 网站上,它按类别列出帖子,但我也在寻找一个列出所有帖子的页面(带分页,每页显示 10 个)。我将如何实现这一目标?

Thanks

谢谢

回答by Gavin

You could create a new page template with this loop in it:

您可以创建一个包含此循环的新页面模板:

<?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( '&larr; Older posts', $wp_query ->max_num_pages); ?>
<?php previous_posts_link( 'Newer posts &rarr;' ); ?>

回答by Simon East

For others who might be Googling this... If you have replaced the front page of your site with a staticpage, but still want your list of posts to appear under a separate link, you need to:

对于可能在谷歌上搜索此内容的其他人...如果您已用静态页面替换了您网站的首页,但仍希望您的帖子列表显示在单独的链接下,您需要:

  1. Create an empty page (and specify any URL/slug you like)
  2. Under Settings > Reading, choose this new page as your "Posts page"
  1. 创建一个空页面(并指定您喜欢的任何 URL/slug)
  2. Settings > Reading 下,选择这个新页面作为您的“帖子页面”

Now when you click the link to this page in your menu, it should list all your recent posts (no messing with code required).

现在,当您单击菜单中指向此页面的链接时,它应该会列出您最近发布的所有帖子(无需弄乱代码)。

回答by lony

A bit more fancy solution based on @Gavins answer

基于@Gavins 答案的更奇特的解决方案

<?php
/*
Template Name: List-all-chronological
*/

function TrimStringIfToLong($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 TrimStringIfToLong(get_the_title()); ?>
    </a>
</li>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts published so far.'); ?></p>
<?php endif; ?>
</ul>