页面模板的分页URL

时间:2014-10-31 作者:Thien Nguyen

我正在WordPress中创建一个页面模板来显示我的所有帖子。但我对分页有问题。我有如下页面url:

http://localhost/sdd/video
“”video“是我的页面slug。问题是当我单击第1页(例如)时,我有url:http://localhost/sdd/video/page/1 但页面显示错误404未找到此页面。谁来帮帮我?谢谢

1 个回复
SO网友:Sabita Sahoo

下面的代码将获取所有类别的所有帖子。在这里,我们将一页限制为10页。然后你必须检查你网站的永久链接结构。请检查代码,我想它会工作的。

     /* it will fetch all posts */
        $post_args = array(
                \'posts_per_page\'  => -1,
                \'orderby\'         => \'post_date\',
                \'order\'           => \'DESC\',
                \'post_type\'       => \'post\',
                \'post_status\'     => \'publish\',
            );
        $all_posts = get_posts( $post_args );
        $post_count = count( $all_posts );

        /* if post_count is greater than 0 */
        if ( $post_count > 0 ) {
            $limit = 10;

            /* this is for getting the last post number */
            if ( get_query_var( \'paged\' ) ) {
                $current_page = get_query_var( \'paged\' );
            } else {
                $current_page = 1;
            }

            $permalink_structure = get_option( \'permalink_structure\' );
            $format              = empty( $permalink_structure ) ? \'&page=%#%\' : \'page/%#%/\';
            $total_pages         = ceil( $post_count / $limit );
            $start               = $current_page * $limit - $limit;

        }

        /* it will fetch posts under limit for all categories */
        $post_args = array(
            \'posts_per_page\'  => $limit,
            \'offset\'          => $start,
            \'orderby\'         => \'post_date\',
            \'order\'           => \'DESC\',
            \'post_type\'       => \'post\',
            \'post_status\'     => \'publish\',
        );

        $current_posts = get_posts( $post_args );
        ?>
        <!-- this is for showing pagination -->
        <div class="tablenav">
            <div class="tablenav-pages">
                <?php
                echo paginate_links(
                    array(
                        \'current\'   => $current_page,
                        \'prev_text\' => \'&laquo; \' . __( \'Prev\' ),
                        \'next_text\'     => __( \'Next\' ) . \' &raquo;\',
                        \'base\'          => add_query_arg( \'paged\', \'%#%\' ),
                        \'format\'    => $format,
                        \'total\'     => $total_pages
                    )
                );
                ?>
            </div>
        </div>
        <!-- then here you can run the loop to show posts from $current_posts -->
<?php

/* if the posts are present */
if ( !empty( $current_posts ) ) {

    foreach ( $current_posts as $post ) {
        /* the code for showing posts */
    }

}

结束

相关推荐

我似乎不能在Get_Pages中显示摘录,并且不知道为什么?

因此,我在get\\u页面中有一个foreach循环,它显示父ID中的子页面列表。标题、特色图片和链接都很好,但我根本无法找到摘录。我在引用http://codex.wordpress.org/Function_Reference/get_pages$args = array( \'child_of\' => 16, \'title_li\' => \'\', \'depth\' => 0,