Hi,
this code is in a WordPress template and does already work to display different custom fields depending on what the referrer page is. But is it possible to use the last word in the post slug i.e,
‘-canvas’
‘-acrylic’
‘-paper’
instead of the whole post URL?
Then I only need the three lines instead of hundreds for all the different store products. As long as the URL ends with these words it displays the correct custom field.
Thankyou
<?php
// 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 == "http://www.website.com/store/man-woman-faces-canvas/" ) {
// if it is, do something
the_field('canvas_description', 'option');
} elseif ( $referer == "http://www.website.com/store/man-woman-faces-acrylic/" ) {
the_field('acrylic_description', 'option');
} elseif ( $referer == "http://www.website.com/store/man-woman-faces-paper/" ) {
the_field('paper_description', 'option');
} else {
// if it isn't, do something else
}
?>
@mcjcentury, I formatted your code, making it easier to read. In the future, if you wish to post code you can format it by either highlighting the code and selecting the </> icon above your edit area, or place three backticks (`) on the line before the code, and three backticks on the line after the code.
hi Webmachine thanks for the tip,
Found the below method which is for the word to exist or not. It works great although Chorns is more precise for the last word which is what I was specifically after. I’ll try that also.
$referer = wp_get_referer();
if (strpos($referer,'canvas') !== false) {
the_field('canvas_description', 'option');
} elseif (strpos($referer,'acrylic') !== false){
the_field('acrylic_description', 'option');
} elseif (strpos($referer,'paper') !== false){
the_field('paper_description', 'option');
} else {
// if it isn't, do something else
}