I am really struggling to find out where or how to introduce paginate to my wordpess post page...
<?php
if (is_tag()) {
$args = array('tag_id' => get_queried_object_id());
} else {
$args = array('cat' => get_queried_object_id());
}
$i=1;
$latest_blog_posts = new WP_Query( $args );
if ( $latest_blog_posts->have_posts() ) : while ( $latest_blog_posts->have_posts() ) : $latest_blog_posts->the_post();
foreach((get_the_category()) as $category) {
$cat_name = $category->cat_name;
$cat_link = get_category_link( $category->term_id );
}
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 2600,1000 ), false, '' );
?>
<div style="height: auto; overflow: auto;">
<div style="float: left; margin-right: 15px;">
<div style="background: url(<?php echo $src[0]; ?> ); width: 330px; height: 260px; background-size: cover; background-repeat:no-repeat;">
</div>
</div>
<div style="float: right; width: 600px;">
<h2 style="margin:0; padding: 0;"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p><span style="font-weight: 200; color: #aaa;">| <?php the_time('j F Y'); ?></span></p>
<p style="line-height: 160%;"><?php echo substr(get_the_excerpt(), 0,500); ?> [...]</p>
<?php echo get_the_tag_list('<p> | ',' | ','</p>'); ?>
<?php $archive_year = get_the_time('Y'); ?>
</div>
</div>
<?php if ( has_post_thumbnail() ) : ?>
<?php else: ?>
<?php endif; ?>
<?php $i++; endwhile; endif; ?>
The file is specifically named category-blog.php
I am not sure where in the script I need to be adding in the pagination, I have tried to understand from where in the codex the examples tie into how I am utilising my content.
Maybe this helps you, i use this code, add it to functions.php
if ( !function_exists( 'wpex_pagination' ) ) {
function wpex_pagination() {
$prev_arrow = is_rtl() ? '→' : '←';
$next_arrow = is_rtl() ? '←' : '→';
global $wp_query;
$total = $wp_query->max_num_pages;
$big = 999999999; // need an unlikely integer
if( $total > 0 ) {
if( !$current_page = get_query_var('paged') )
$current_page = 1;
if( get_option('permalink_structure') ) {
$format = 'page/%#%/';
} else {
$format = '&paged=%#%';
}
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => $format,
'current' => max( 1, get_query_var('paged') ),
'total' => $total,
'mid_size' => 3,
'type' => 'list',
'prev_text' => $prev_arrow,
'next_text' => $next_arrow,
) );
}
}
}
You can use it in your category-blog.php like:
<div class="pagination">
<?php wpex_pagination(); ?>
</div>
You need to add the paged parameter to your args array. The paged
variable is retrieved from the page query var which you can get like this:
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
You would then probably want to limit the number of posts per page using the posts_per_page
parameter. So your args array would be something like this:
$args = array(
'paged' => $paged,
'posts_per_page' => 5
);
Wordpress has a built in function to display pagination links that quite well out the box called paginate_links
http://codex.wordpress.org/Function_Reference/paginate_links .
So here is how I fixed it...
<?php
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
if (is_tag()) {
$args = array(
'tag_id' => get_queried_object_id(),
'posts_per_page' => 4,
'paged' => $paged
);
} else {
$args = array(
'cat' => get_queried_object_id(),
'posts_per_page' => 4,
'paged' => $paged
);
}
$i=1;
$latest_blog_posts = new WP_Query( $args );
if ( $latest_blog_posts->have_posts() ) : while ( $latest_blog_posts->have_posts() ) : $latest_blog_posts->the_post();
foreach((get_the_category()) as $category) {
$cat_name = $category->cat_name;
$cat_link = get_category_link( $category->term_id );
}
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 2600,1000 ), false, '' );
?>
<?php endif; ?>
<div style="height: auto; overflow: auto; margin-bottom: 20px;">
<div style="float: left; margin-right: 15px;">
<div style="background: url(<?php echo $src[0]; ?> ); width: 330px; height: 260px; background-size: cover; background-repeat:no-repeat;"></div>
</div>
<div style="float: right; width: 600px;">
<h2 style="margin:0; padding: 0;"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p><span style="font-weight: 200; color: #aaa;">| <?php the_time('j F Y'); ?></span></p>
<p style="line-height: 160%;"><?php echo substr(get_the_excerpt(), 0,500); ?> [...]</p>
<?php echo get_the_tag_list('<p> | ',' | ','</p>'); ?>
<?php $archive_year = get_the_time('Y'); ?>
</div>
</div>
<?php if ( has_post_thumbnail() ) : ?>
<?php else: ?>
<?php endif; ?>
<?php $i++; endwhile; endif; ?>
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $latest_blog_posts->max_num_pages
) );
?>
Also in the wp-admins etting I set under 'reading' the 'Blog pages show at most' and set it to 4 to reflect the above, hey presto it appeared!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.