Try it with a query like this:
<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$the_query = new WP_Query( 'posts_per_page=20&post_type=portfolio&paged=' . $paged );
?>
<?php
while ( $the_query->have_posts() ) : $the_query->the_post();
$featuredImage = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) );
$permalink = get_permalink( $id );
// -- Loop stuff --
?>
<?php endwhile; ?>
<nav class="paged text-center">
<?php previous_posts_link( '« Newer' ); ?>
<?php next_posts_link( 'Older »', $the_query->max_num_pages ); ?>
</nav>
<?php wp_reset_postdata(); ?>
http://codex.wordpress.org/Function_Reference/next_posts_link#Usage_when_querying_the_loop_with_WP_Query
Thread Starter
Szyam
(@szyam)
Thanks, Ill try this. When it says to use ‘page’ for for static front page their referring to the two ‘paged’ in single quotes and not the variable ‘$paged’ correct?
That’s correct. If it’s a static front page use:
$paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
Thread Starter
Szyam
(@szyam)
Hey keesiemeijer, this code has got me further. The ‘Page’ vs ‘Paged’ thing was something I was not aware of. I was just using WP’s ‘front-page.php’ for showing a custom front page, but see that this ‘page’ is for when using the ‘static’ front page option in Settings/Reading.
So, after creating a page that calls a template, where I now have this code being called, I get the static front page showing 20 posts, when I click on ‘Older’, it displays the last 20 posts but no way to access posts in the middle? And when on page/2 which is showing the last 20 posts, there is only the ‘Older’ link which refreshes that page. The ‘Newer’ link is not showing up? Any ideas? Thanks so much!
Thread Starter
Szyam
(@szyam)
Apparently some of my previous code worked when I replaced the ‘paged’ with ‘page’. I’ve been working with WordPress for over a year and haven’t encountered this caveat while working with CPT’s. I’d prefer to use WP’s front-page.php vs a template, however when I use that, with ‘paged’ for the variable it still throws a 404. A battle for another day.
I’m going to paste my working code here, for others who may find this problem. Note: I am using some additional code in my functions.php file from 320press foundation framework which I have used on a few projects. It contains the necessary code for the page_navi. Again keesiemeijer, thanks so much for pointing this out!!
<?php $paged = (get_query_var('page')) ? get_query_var('page') : 1;
//$parent = $post->ID; /* Show child pages - Delete if not needed */
$args = array('posts_per_page' => 20,'post_type' => 'portfolio', 'paged' => $paged );
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
$featuredImage = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
$permalink = get_permalink( $id );
?>
<!– Loop Stuff –>
<?php endwhile; endif; ?>
<?php if (function_exists('page_navi')) { ?>
<?php page_navi(); ?>
<?php } else { ?>
<nav class="wp-prev-next">
<div class="page-nav">
<div class="nav-previous"><?php next_posts_link('<< Next'); ?></div>
<div class="nav-next"><?php previous_posts_link('Back >>'); ?></div>
</div>
</nav>
<?php } ?>
<?php wp_reset_query(); ?>
Thread Starter
Szyam
(@szyam)
Well I spoke slightly too soon. The numbered pagination was working accordingly, ie: clicking on 3 took me to page 3 and so on. However, the next and previous links did not work. Only pointed at page 2. I’ve managed to find a post that provided a solution but I feel its a little more heavy handed then should be. I’ll provide it below but if you (or someone else) may have another idea/answer I’d love to hear it. Thanks!
<?php
//Fix homepage pagination
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } else if ( get_query_var('page') ) {$paged = get_query_var('page'); } else {$paged = 1; }
$temp = $wp_query; // re-sets query
$wp_query = null; // re-sets query
$args = array( 'post_type' => 'portfolio', 'orderby'=>'date', 'order'=>'DESC', 'posts_per_page' => 20, 'paged' => $paged);
$wp_query = new WP_Query();
$wp_query->query( $args );
while ($wp_query->have_posts()) : $wp_query->the_post();
$featuredImage = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
$permalink = get_permalink( $id );
?>
<!– Loop Stuff –>
<?php endwhile; ?>
<nav>
<?php paginate();
$wp_query = null;
$wp_query = $temp; // Reset
?>
</nav>
Then this needs to be placed into the functions.php
function paginate() {
global $wp_query, $wp_rewrite;
$wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
$pagination = array(
'base' => @add_query_arg('page','%#%'),
'format' => '',
'total' => $wp_query->max_num_pages,
'current' => $current,
'show_all' => true,
'type' => 'list',
'next_text' => 'Next »',
'prev_text' => '« Previous'
);
if( $wp_rewrite->using_permalinks() )
$pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 'page', get_pagenum_link( 1 ) ) ) . '?page=%#%/', 'paged' );
if( !empty($wp_query->query_vars['s']) )
$pagination['add_args'] = array( 's' => get_query_var( 's' ) );
echo paginate_links( $pagination );
}