Solved

How to determine if Previous Post or Next Post exist in Wordpress?

Posted on 2014-10-27
7
215 Views
Last Modified: 2014-11-06
In a Wordpress blog, I needed a way for a post to be able to determine if there's either a previous post or a next post that exist before or after it.

I've got a way to do it, but it seems unnecessarily complex and I was just wondering if someone had a more elegant way to accomplish the same thing?

Here's the example of how I'm currently determining if a previous post exists:
if (strlen(get_previous_post()->post_title) > 0)

Open in new window

0
Comment
Question by:Brian Matis
  • 3
  • 3
7 Comments
 
LVL 25

Accepted Solution

by:
James Rodgers earned 500 total points
ID: 40408219
you can do this instead

if (get_previous_post()){
// do true
}else{
//do false
}

Open in new window


the get_previous_post and get_next_post functions return Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
0
 
LVL 6

Author Comment

by:Brian Matis
ID: 40408950
Thanks! I'll give this a try when I get a chance, but I seem to recall trying something like this at first and having a problem because the empty string didn't actually evaluate to false.
0
 
LVL 15

Expert Comment

by:eemit
ID: 40412974
See how it is done in Twenty Fourteen Theme:

/**
 * Display navigation to next/previous post when applicable.
 *
 * @since Twenty Fourteen 1.0
 */
function twentyfourteen_post_nav() {
	// Don't print empty markup if there's nowhere to navigate.
	$previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
	$next     = get_adjacent_post( false, '', false );

	if ( ! $next && ! $previous ) {
		return;
	}

...

Open in new window

0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 6

Author Comment

by:Brian Matis
ID: 40419797
Sorry, still haven't had a chance to go and try this out. Should be able to some evening this week and then I'll report back on how it goes.
0
 
LVL 25

Expert Comment

by:James Rodgers
ID: 40419805
no worries, let us know if you need anything else.
0
 
LVL 6

Author Closing Comment

by:Brian Matis
ID: 40426986
Yep, Jester_48's solution totally worked! I must have done something wrong when I had first tried a similar solution. Now my code is much cleaner. Thanks!
0
 
LVL 25

Expert Comment

by:James Rodgers
ID: 40427031
glad I could help.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
The viewer will learn how to dynamically set the form action using jQuery.

786 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