Solved

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

Posted on 2014-10-27
7
211 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Php pie charts 3 26
WordPress syntax 2 26
PHP warning 4 30
updating the date data 12 21
This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 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…

910 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

18 Experts available now in Live!

Get 1:1 Help Now