Solved

Wordpress WP_Query with pagnation

Posted on 2014-01-20
1
207 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
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
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 purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
The viewer will learn how to dynamically set the form action using jQuery.

756 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