WordPress Pagination Problems - disappearing next_posts_link - php

I was hoping someone could help me address an issue I've been having trying to paginate a custom template in WordPress. I've scoured through Stackoverflow and other website forums looking for answers related to this, but no one seems to have problems exactly like the one I'm having.
I am currently making changes to a WordPress site built by a predecessor and on one of the pages shows our company portfolio. This showed all the posts we have published (as work) until I recently changed it to 12 per page. However, I can't seem to get the pagination to work or the posts_links top even appear on the page.
The page in question is http://thefabl.com/our-work and the code I currently have on the page (just from the top of the loop to the footer), is as stands below:
<?php
$cases = 1;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'work',
'posts_per_page' => 12,
'post_status' => 'publish',
'paged' => $paged,
'orderby' => 'date',
);
if ( isset( $_GET[ 'project' ] ) ) {
$args[ 'project' ] = $_GET[ 'project' ];
}
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();
?>
<li <?php if ( ( $cases % 3 ) == 0 ) { echo ' class="end"'; } ?>>
<div class="overlay absolute-full">
</div>
<figure>
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail( 'work_list' );
} else {
echo '<img src="http://placehold.it/298x175" alt="Placeholder"/>';
}
?>
</figure>
<div class="post-meta">
<p class="title">
<?php the_title(); ?>
</p>
<span class="work-type">
<?php the_field( 'work_type' ); ?>
</span>
</div>
</li>
<?php
$cases++;
endwhile; ?>
<div class="main" id="post-pagination">
<?php if( has_previous_posts() ){ ?><div class="prev"><?php echo previous_posts_link( 'Previous Page' ); ?></div><?php } ?>
<?php if( has_next_posts() ){ ?><div class="next"><?php echo next_posts_link( 'Next Page' ); ?></div><?php } ?>
</div>
<?php endif; ?>
</ul>
</div>
<a class="back-to-top bottom center"></a>

Perhaps echo causing the problem. Please use following lines.
<div class="next"><?php next_posts_link('« Older Entries') ?></div>
<div class="prev"><?php previous_posts_link('Newer Entries »') ?></div>
Hope it will work for you.
and if you are showing posts on homepage, you may need to replace
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
with
<?php
if ( get_query_var('paged') )
{
$paged = get_query_var('paged');
}
else if ( get_query_var('page') )
{
$paged = get_query_var('page');
}
else
{
$paged = 1;
}

Related

Wordpress second page pagination not showing posts

I've been trying to add numeric pagination to my custom wordpress theme. I have run into a problem where I can't see any posts on second(or third page). I have my page-archive.php file, index.php, single.php files all set up. It should be everything that a blog site needs? I'm a bit confused what am I missing here? I've been trying multiple different options and I tried to modify my page-archive.php page but no luck.
index.php
<div class="blogitem a">
<?php
//PRINT ONLY Tutvustus
$lastBlog = new WP_Query('type=post&posts_per_page=3');
if( $lastBlog->have_posts() ):
while( $lastBlog->have_posts() ): $lastBlog->the_post(); ?>
<?php get_template_part('page-archive',get_post_format()); ?>
<?php endwhile;
endif;
wp_reset_postdata();
?>
<div class="pagination">
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
query_posts(array(
'post_type' => 'post', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 2
));
if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php // post content goes here ?>
<?php endwhile; ?>
<?php my_pagination(); ?>
<?php else : ?>
<?php // no posts found message goes here ?>
<?php wp_reset_query(); // add this ?>
<?php endif; ?>
</div>
</div>
functions.php
if ( ! function_exists( 'my_pagination' ) ) :
function my_pagination() {
global $wp_query;
$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' => $wp_query->max_num_pages
) );
}
endif;
page-archive.php
<?php
/*
Template Name: Archives
*/
get_header(); ?>
<div id="container">
<div id="content" role="main">
<?php the_post(); ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
</div><!-- #content -->
</div><!-- #container -->
<?php get_footer(); ?>
It turns out that I don't need the first half of the code. Feeling dumb. Anyways here is the answer.
<div class="pagination">
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
query_posts(array(
'post_type' => 'post', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 2
));
if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part('catalog',get_post_format()); ?> // added template part here and voila it works
<?php endwhile; ?>
<?php my_pagination(); ?>
<?php else : ?>
<?php ?>
<?php wp_reset_query(); // add this ?>
<?php endif; ?>
</div>

How to add next_post_link to the following loop

I've tried to add the <?php next_posts_link('Read more »', 10); ?> but it doesn't work with this loop. How can I solve this case? I want to show 10 posts and read more link for the next 10 posts etc. Thanks.
<div id="posts" class="row">
<?php $loop = new WP_Query( array( 'post_type' => 'post', 'orderby' => 'post_id', 'posts_per_page' => '10', 'order' => 'DESC' ) ); ?>
<?php while( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="item col-sm-4">
<div class="well">
<a href="<?php the_permalink(); ?>"><?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?></a>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<div class="readmore-wrapper">
<a class="readmore" href="<?php the_permalink(); ?>">Read more</a>
</div>
</div></div>
<?php endwhile; wp_reset_query(); ?>
</div>
You need to use the paged variable to tell where you are in the paginated list.
Add this line before your loop: $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : '1'; and then add this to your query args: 'nopaging' => false, 'paged' => $paged,
You can then use previous_posts_link( '« Newer Entries' ); and next_posts_link( 'Older Entries »', $query->max_num_pages ); to get your previous/next links before and after the while/endwhile loop respectively.
BTW, your while loop should ideally be wrapped in an if ( $loop->have_posts() ) statement.
Hope that helps
Try this with pagination.
<?php
$paged = ( isset( $_GET['pg'] ) && intval( $_GET['pg'] ) > 0 )? intval( $_GET['pg'] ) : 1;
query_posts( array( 'post_type' => 'post', 'paged' => $paged, 'posts_per_page' => 10 ) );
?>
<?php if ( have_posts() ) : ?>
<?php while( have_posts() ) : the_post(); ?>
<div id="post-<?php echo $post->ID; ?>" <?php post_class(); ?>>
<h3><?php the_title(); ?></h3>
<div class="post-excerpt">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div class="pagination">
<?php for ( $i = 1; $i <= $wp_query->max_num_pages; $i ++ ) {
$link = $i == 1 ? remove_query_arg( 'pg' ) : add_query_arg( 'pg', $i );
echo '<a href="' . $link . '"' . ( $i == $paged ? ' class="active"' : '' ) . '>' . $i . '</a>';
} ?>
</div>
<?php endif ?>
<?php else : ?>
<div class="404 not-found">
<h3>Not Found</h3>
<div class="post-excerpt">
<p>Sorry, but there are no more posts here... Please try going back to the main page</p>
</div>
</div>
<?php endif;
// Make sure the default query stays intact
wp_reset_query();

Pagination for Custom Loop (loop with foreach)

I'm trying to add pagination to a custom loop, but I can't figure out how to do it. When I manage to add "previews" and "next" buttons it always shows the same 10 posts. I found some solutions for a while loop but not for a foreach loop (which I actually never used before).
This is the loop (is get_posts a problem?) :
<?php
$news = get_posts(array('posts_per_page' => 10));
$news['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; ?>
<?php foreach ($news as $article): ?>
<div class="col-md-4">
<h3><?php echo $article->post_title ?></h3>
<hr>
<p class="desc"><?php echo $article->post_excerpt ?></p>
<?php echo get_the_post_thumbnail($article->ID,'thumbnail'); ?>
<p class="btn_text"> Ler mais</p>
</div>
<?php endforeach; ?>
<?php previous_posts_link( '<<' );
next_posts_link( '>>', $custom_query->max_num_pages ); ?>
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$custom_query_args = array(
'post_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date' // modified | title | name | ID | rand
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<article <?php post_class(); ?>>
<h3><?php the_title(); ?></h3>
<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
<div><?php the_excerpt(); ?></div>
</article>
<?php
endwhile;
?>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $custom_query;
?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $custom_query->max_num_pages ); ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); ?>
</div>
</nav>
<?php
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php
wp_reset_postdata(); // reset the query
else:
echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>
Source : http://web-profile.net/wordpress/themes/wordpress-custom-loop/

WP_Query Pagination not working with custom query on custom post type

I have read as many other problems like mine and not been able to find a fix. For some reason my custom query is not paginating and I can not work out why. It is probably a silly reason but hope you can help.
Here is my code.
<?php
global $wp_query;
$title = $wp_query->post->post_title;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query = new WP_Query(
array(
'paged' => $paged,
'wpse18703_title' => "$title",
'posts_per_page' => 10,
'post__not_in' => array($post->ID),
'post_type' => array('post', 'features'),
)
);
if ($query->have_posts()){
while ( $query->have_posts() ) { $query->the_post(); ?>
<a href="<?php echo get_permalink(); ?>">
<div class="small-12 large-12 gdb-news-container">
<div class="small-12 large-4 columns gdb-news-image">
<?php the_post_thumbnail('game-news-thumb'); ?>
</div>
<div class="small-12 large-8 columns game-title">
<h5><?php the_title(); ?></h5>
<?php echo content(20); ?>
</div>
<div class="clearfix"></div>
</div>
</a>
<?php
} //end while
wp_reset_query();
} //end if
?>
<?php if ( function_exists('reverie_pagination') ) { reverie_pagination(); } else if ( is_paged() ) { ?>
<nav id="post-nav">
<div class="post-previous"><?php next_posts_link( __( '← Older posts', 'reverie' ) ); ?></div>
<div class="post-next"><?php previous_posts_link( __( 'Newer posts →', 'reverie' ) ); ?></div>
</nav>
<?php } ?>
<?php
if(isset($_GET['paged']) && !empty($_GET['paged'])):endif;
global $wp_query;
$temp = $wp_query;
$wp_query = null;
$title = $wp_query->post->post_title;
$args = array(
'paged' => $paged,
'wpse18703_title' => "$title",
'posts_per_page' => 10,
'post__not_in' => array($post->ID),
'post_type' => array('post', 'features')
);
$wp_query = new WP_Query($args);
if($wp_query->have_posts()){
while($wp_query->have_posts()){
$wp_query->the_post(); ?>
<a href="<?php echo get_permalink(); ?>">
<span class="small-12 large-12 gdb-news-container">
<span class="small-12 large-4 columns gdb-news-image">
<?php the_post_thumbnail('game-news-thumb'); ?>
</span>
<span class="small-12 large-8 columns game-title">
<h5><?php the_title(); ?></h5>
<?php echo content(20); ?>
</span>
</span>
</a>
<?php }
}
$wp_query = null;
$wp_query = $temp;
if(function_exists('reverie_pagination')){reverie_pagination();} else if (is_paged()){ ?>
<nav id="post-nav">
<div class="post-previous"><?php next_posts_link( __( '← Older posts', 'reverie' ) ); ?></div>
<div class="post-next"><?php previous_posts_link( __( 'Newer posts →', 'reverie' ) ); ?></div>
</nav><?php
}
?>
I changed you div's with the classes "small-12" to span's because it is invalid W3C code to put a div inside an anchor tag, so you might have to modify your css file, depending on how it was coded.
I think you are using your $paged variable wrong (i am guessing you are using it inside "pages" not "posts"). This should work both on pages and posts:
if ( get_query_var( 'paged' ) ) {
$paged = absint( get_query_var( 'paged' ) );
} elseif ( get_query_var( 'page' ) ) {
$paged = absint( get_query_var( 'page' ) );
} else {
$paged = 1;
}

How do I reverse the order in PHP when Echoing

The goal here is to reverse the echoed php results so that the very first is the very last number.
I want the weeks to go 3-1 instead of 1-3 but for new posts to follow as well.
<?php
/* Get posts for home page */
if(is_page_template('template-home.php')):
$args = array(
'post_type'=>'post'
);
if(is_front_page()){
$args['paged'] = get_query_var('page');
global $paged;
$paged = $args['paged'];
} else {
$args['paged'] = get_query_var('paged');
}
$wp_query = new WP_Query($args);
endif;
?>
<div id="scroll-menu" class="scroll-menu"> <span class="week">WEEK</span>
<?php if ( have_posts() ) : $i=0; while ( have_posts() ) : the_post(); $i++; ?>
<div id="scroll-post-<?php the_ID(); ?>" data-id="post-<?php the_ID(); ?>" class="scroll-post">
<span><?php echo $i; ?></span>
<div class="scroll-menu-title"><?php the_title(); ?></div>
</div>
<?php endwhile; ?>
<?php endif;?>
<?php wp_reset_query(); ?>
</div>
Use
$args = array(
'post_type'=>'post'
'order' => 'ASC',
'orderby' => 'date'
);

Categories