首页 文章

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

提问于
浏览
20

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

谢谢

3 回答

  • 46

    您可以使用此循环创建新的页面模板:

    <?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;' ); ?>
    
  • 14

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

    • 创建一个空页面(并指定您喜欢的任何URL / slug)

    • Settings > Reading 下,选择此新页面作为"Posts page"

    现在,当您单击菜单中此页面的链接时,它应列出您最近的所有帖子(不需要处理代码) .

  • 2

    基于@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>
    

相关问题