Solved

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

Posted on 2014-10-27
7
233 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
[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
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Independent Software Vendors: 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…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

632 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