WordPress – Conditional Tag based on wildcard

I know how to do wordpress conditional statements based on a page ID, page name or url slug, but I’m looking to do something slightly different. I’m looking for a conditional statement for my 404 page that will help me display different content if the URL begins with mysite.com/wiki/.

Is there some kind of wildcard I could use, like * or % or something like that?

Related posts

Leave a Reply

1 comment

  1. Welcome to StackOverflow.

    I believe this is what you are looking for on your 404 page:

    $uri = strtolower($_SERVER['REQUEST_URI']); // something like "/post/category/1234" or "/wiki/title" or "/" 
    
    if (strpos($uri, '/wiki') === 0) {
        echo "You are accessing something at yoursite.com/wiki/*";
    } else {
        echo "Just some non-wiki URL!";
    }
    

    strpos() will return false if the search text was not found, or the 0-based index that the match occurred at in the string.

    We are checking to see if ‘/wiki’ was at the beginning of $uri, and if so it outputs one message, if not, it outputs another.