user692100
user692100

Reputation: 1

Conditional check for previous page before showing content in WordPress

I'm not sure if this is possible, but I have a question that I would like to get answered. What I have done is made a custom loop on certain pages. The items in this loop have many categories, and I have setup a function to display those categories on the single page. However, from a few particular pages, I just want a static category to be displayed instead of all the possible categories. Is there a way to check the previous page and apply something based on that, kind of like this:

if (is_single() && previous_page_is('about')) { 
//stuff here
}

Obviously there is not a previous_page_is tag, but is there something that I can do that would mimic this functionality?

Thanks, Thomas

Upvotes: 0

Views: 1967

Answers (2)

Rebecca Hill
Rebecca Hill

Reputation: 11

I realise this was asked 3 years ago but I found this post while I was searching for the same thing - I found the answer so figured I would post it here. With thanks to http://zoerooney.com/blog/tutorials/using-the-referring-page-in-wordpress/

// use the WordPress tag wp_get_referer to assign the referring URL to the variable $referer
$referer = wp_get_referer();

// check if the URL is a specific one
if ( $referer == "whatever-the-full-about-page-url-is" ) {

  // if it is, do something

} else {

  // if it isn't, do something else

}

Upvotes: 1

Rochester Oliveira
Rochester Oliveira

Reputation: 1483

I think you could use $_SERVER['HTTP_REFERER'] ( http://www.php.net/manual/en/reserved.variables.server.php ) and then if it is needed use get_posts or get_page to retrieve the page that as this slug..

Upvotes: 0

Related Questions