WP Conditional Tags

Hi,

At the moment I have some widget logic to display content on special pages like:

( ( is_single() && ( in_category( 'mauritius' ) ) ) || ( ( is_tag('mauritius-beachcomber') ) ) || ( ( is_category( 'mauritius' ) ) ) )

I like to add additionally a logic like “URL contains /mauritius/”.

Can you help me with that? The text field let me use WP Conditional Tags, or any general PHP code.

Michael

Hello Michael,

Thanks for posting in!

Regretfully there isn’t a condition that checks the url of the page or post.
You might just update your conditions into this:

( ( is_single() && in_category( 'mauritius' ) ) || is_page('mauritius') ||  is_tag('mauritius-beachcomber') ||  is_category( 'mauritius' ) )

The rest of the available WP Conditional tags can be found here:

Hi,

thank you for the update.

But this logic is not working on these site:


Is it possible via PHP code? If found this code but I don´t know if this the complete code - for expample the last { in this example:

if(strpos($_SERVER[‘REQUEST_URI’], ‘mauritius’) !== false){

Hey Michael,

Your code seems correct, you can do something like this in PHP to find a string in a URL:

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'mauritius') !== false) {
    echo 'mauritius exists.';
}

Alternatively you use a short way:

if (strpos($_SERVER['REQUEST_URI'], "mauritius") !== false){
// mauritius exists
}

So your conditional check will look like this:

( ( is_single() && in_category( 'mauritius' ) ) || is_page('mauritius') ||  is_tag('mauritius-beachcomber') ||  is_category( 'mauritius' ) ||  (strpos($_SERVER['REQUEST_URI'], "mauritius") !== false) )

Hope this helps!

Perfect, you´re the best!! :slight_smile:

Glad we could help.

Cheers!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.