Solved

Wordpress WP_Query with pagnation

Posted on 2014-01-20
1
209 Views
Last Modified: 2014-02-15
Hey everyone.

So I'm desperately trying to learn/understand the code when using a loop in wordpress.  I used to use query_posts instead of WP_Query which I now understand is bad practice. I've read through the wordpress codex several times and I think I have a good understanding of the loop now but I'm trying to understand how to use the rest.

So as of now this is my loop

<?php 

	$args = array( 
		'post_type' => 'post',
		'order' => 'DSC',
		'posts_per_page' => '2',
	);
	$the_query = new WP_Query( $args );

?>

<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>


	<h1><?php the_title() ;?></h1>			
	<?php the_content() ;?>

<?php endwhile; else: ?>

	<p>Sorry, there are no posts to display</p>

<?php endif; ?>

<?php previous_posts_link() ;?><?php next_posts_link() ;?>

Open in new window


The problems I'm running into is:

A. the previous and next posts button isn't showing (and yes I have more than two posts ;) )

B. if you go to mysite.com/page/2/ it still shows the latest two posts.

I think I need to add in some sort of $paged but I'm not sure where to implement it.
0
Comment
Question by:Pancake_Effect
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 17

Accepted Solution

by:
OmniUnlimited earned 500 total points
ID: 39794868
Pagination links are supposed to be placed after the endwhile and before the else in the loop as follows:

<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>


	<h1><?php the_title() ;?></h1>			
	<?php the_content() ;?>

<?php endwhile; ?>

<?php previous_posts_link() ;?><?php next_posts_link() ;?> 

<?php else: ?>

	<p>Sorry, there are no posts to display</p>

<?php endif; ?>

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question