Is there a way to filter single.php so that if someone came from search.php it would display one thing, but if they came from anywhere else it would display another?
Thanks, Josh
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityIs there a way to filter single.php so that if someone came from search.php it would display one thing, but if they came from anywhere else it would display another?
Thanks, Josh
You could hook into the the_permalink
filter and check for is_search()
to conditionally modify the URLs of your search results to include some additional parameters that your single.php
then checks for. Something like this (may require tinkering):
add_filter( 'the_permalink', 'wpse155331_the_permalink' );
function wpse155331_the_permalink( $url ) {
if ( is_search() ) {
$url = add_query_arg( array( 'from_search' => 'true' ), $url );
}
return $url;
}
add_query_arg
function.
Jul 21, 2014 at 21:40
I created an if
statement to solve my problem:
<?
if (stripos(wp_get_referer(), "cat=")!==false) {
echo "Came from Search";
} else {
echo "Didn't come from Search";
}
?>
I am doing a category search, so I check to see if cat= is in the referrer url (or search url), if it is I execute some code, if not I execute some different code :-)
That's it!
Thanks,
Josh