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
Solved

Wordpress WP_Query with pagnation

Posted on 2014-01-20
1
206 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The purpose of this video is to demonstrate how to Import and export files in WordPress. 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 : Click on Too…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

840 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