Wordpress - List all posts (with proper_pagination

2019-01-16 13:15发布

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?

Thanks

标签: wordpress
3条回答
Bombasti
2楼-- · 2019-01-16 13:28

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;' ); ?>
查看更多
男人必须洒脱
3楼-- · 2019-01-16 13:37

For others who might be Googling this... If you have replaced the front page of your site with a static page, 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"

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

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-16 13:49

A bit more fancy solution based on @Gavins answer

<?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>
查看更多
登录 后发表回答