Solved

Wordpress WP_Query with pagnation

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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now