Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Wordpress WP_Query with pagnation

Posted on 2014-01-20
1
Medium Priority
?
211 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 2000 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

Technology Partners: 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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
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…

719 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