I need to build an archive page for about 30.000 posts. I tried using the standard archive method of wordpress but it doesn't quite cover what I want to do.
This is what I have so far:
<?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 -->
Now I have all the posts in one page which isn't really easy to use. So I need to have pagination to give more of an overview. I know it must be possible but I really have no clue on how to go about this.
Really hope someone can help me and some people in the future with this. Maybe there also is another solution to this that I just couldn't find.
Alter arguments:
$args = array(
'post_type' => 'post',
'posts_per_page' => 50,
'paged' => 1
);
For pagination this should work:
while( $post_query->have_posts() ){
// your code
}
next_posts_link( 'Older Entries', $post_query ->max_num_pages );
previous_posts_link( 'Newer Entries' );
WordPress pagination documentation.
You can read documentation for custom WordPress query.
Please use this code:
<?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();
?>
Now define this pagination function in your theme's 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' => __( '← Previous', '' ),
'next_text' => __( 'Next →', '' ),
'type' => 'list',
) );
if ( $links ) :
echo $links;
endif;
}