用分页为wordpress构建一个存档页面


building an archive page for wordpress with pagination

我需要为大约30,000个帖子建立一个存档页面。我尝试使用wordpress的标准存档方法,但它不能完全覆盖我想要做的。

这是我目前为止写的:

<?php
/*
Template Name: Archives
*/
get_header(); ?>
<div id="container">
<div id="content" role="main">
    <?php
        // Get total number of posts for pagination
        $count_posts = wp_count_posts();
        $published_posts = $count_posts->publish;
        $args = array(
            'post_type' => 'post',
            'posts_per_page' => 50
        );
        $post_query = new WP_Query($args);
        ?>
        <table style="width:100%">
        <tr>
            <th>Post</th>
            <th>Datum</th>
            <th>Categorie</th>
        </tr>
        <?php
        if($post_query->have_posts() ) {
            while($post_query->have_posts() ) {
                echo "<tr>";
                $post_query->the_post();
                ?>
                <td><a href="<?php the_permalink() ?>" rel="bookmark" title="Link naar <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                <?php
                echo "<td>" . get_the_date() . "</td>";
                $category_list = get_the_category_list( ', ' );
                if ( $category_list )
                    echo "<td>" . $category_list . "</td>";
                echo "</tr>";
            }
        }
        echo "</table>";
    ?>
</div><!-- #content -->

现在我把所有的帖子放在一个页面上,这不是很容易使用。所以我需要分页来给出更多的概述。我知道这一定是可能的,但我真的不知道该怎么做。

真的希望有人能帮助我和一些人在这个未来。

修改参数:

$args = array(
    'post_type' => 'post',
    'posts_per_page' => 50,
    'paged' => 1
);

对于分页,这应该可以工作:

while( $post_query->have_posts() ){
    // your code
}
next_posts_link( 'Older Entries', $post_query ->max_num_pages );
previous_posts_link( 'Newer Entries' );

WordPress分页文档

请使用此代码:

<?php
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => 50
    );
    $post_query = new WP_Query($args);
    if($post_query->have_posts() ) {
        while($post_query->have_posts() ) {
            echo "<tr>";
            $post_query->the_post();
            ?>
            <td><a href="<?php the_permalink() ?>" rel="bookmark" title="Link naar <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
            <?php
            echo "<td>" . get_the_date() . "</td>";
            $category_list = get_the_category_list( ', ' );
            if ( $category_list )
                echo "<td>" . $category_list . "</td>";
            echo "</tr>";
        }
    }
    echo "</table>";
   pagination_nav();
?>

现在在主题的functions.php

中定义这个分页函数
function pagination_nav(){
// Don't print empty markup if there's only one page.
if ( $GLOBALS['wp_query']->max_num_pages < 2 ) {
    return;
}
$paged        = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
$pagenum_link = html_entity_decode( get_pagenum_link() );
$query_args   = array();
$url_parts    = explode( '?', $pagenum_link );
if ( isset( $url_parts[1] ) ) {
    wp_parse_str( $url_parts[1], $query_args );
}
$pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
$pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
$format  = $GLOBALS['wp_rewrite']->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
$format .= $GLOBALS['wp_rewrite']->using_permalinks() ? user_trailingslashit( 'page/%#%', 'paged' ) : '?paged=%#%';
// Set up paginated links.
$links = paginate_links( array(
    'base'     => $pagenum_link,
    'format'   => $format,
    'total'    => $GLOBALS['wp_query']->max_num_pages,
    'current'  => $paged,
    'mid_size' => 3,
    'add_args' => array_map( 'urlencode', $query_args ),
    'prev_text' => __( '&larr; Previous', '' ),
    'next_text' => __( 'Next &rarr;', '' ),
    'type'      => 'list',
) );
if ( $links ) :
    echo $links;
endif;
    }