如何将分页添加到我的自定义页面模板

时间:2017-02-17 10:56:04

标签: wordpress pagination

我刚创建了一个新的自定义页面模板,只调用了两个帖子。 如何添加分页以便我可以与至少两个最新帖子建立链接?

我尝试了这段代码,但它不起作用:

<?php
    $paged = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1;
    query_posts( 
        array ( 
            'post_type' => 'post', 
            'category_name' => 'news', 
            'category' => 1,
            'posts_per_page' => 2,
            'paged' => $paged ) 
        );      
        // The Loop
        while ( have_posts() ) : the_post();?>
            <div class="news-page-content-wrapper">
                <div class="news-page-content">
                    <h1><a class="read-more"href="<?php the_permalink(); ?>"><?php the_title();?></a></h1>
                    <figure><?php the_post_thumbnail(); ?></figure>
                    <p><?php echo get_the_excerpt();?></p>
                    <a href="<?php the_permalink(); ?>">Read More&raquo</a>
                </div>
            </div>   
        <?endwhile; 
        // Reset Query
        wp_reset_query();
    ?>

任何帮助?

1 个答案:

答案 0 :(得分:0)

由于你正在使用“循环”,你应该使用内置函数来显示分页。

以下是您的一些示例:https://codex.wordpress.org/Pagination

我已更新您的示例代码以显示默认分页:

<?php
    $paged = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1;
    query_posts( 
        array ( 
            'post_type' => 'post', 
            'category_name' => 'news', 
            'posts_per_page' => 2,
            'paged' => $paged ) 
        );      
        // The Loop
        while ( have_posts() ) : the_post(); ?>
            <div class="news-page-content-wrapper">
                <div class="news-page-content">
                    <h1><a class="read-more"href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
                    <figure><?php the_post_thumbnail(); ?></figure>
                    <p><?php echo get_the_excerpt(); ?></p>
                    <a href="<?php the_permalink(); ?>">Read More&raquo; </a>
                </div>
            </div>   
        <?php endwhile; 

        the_post_navigation();
        // Reset Query
        wp_reset_query();
    ?>
相关问题