Support » Plugins » Hacks » Conditional Tag for any page with a particular word in the title?

  • I know how to do 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 any page that has a given word in the title (or slug would work, too).

    I’ve tried:
    <?php if ( is_page('keyword') ) { ?>
    …thinking that it might work because that keyword is in the title and slug, but it didn’t work. Is there some kind of wildcard I could use, like * or % or something like that?


Viewing 15 replies - 1 through 15 (of 23 total)
  • if( strpos( $post->post_title, 'computers' ) )
    		echo 'title has word computers';

    Thanks Michael! However, I can’t get it to work. I’m trying to put something in a PHP Code Widget in the sidebar, that displays something if the page name contains ‘keyword’.

    I can get it to work with the page name, but using the code you provided, I get the following error:

    Parse error: syntax error, unexpected T_ELSE in /home/user/public_html/wp-content/plugins/php-code-widget/execphp.php(44) : eval()'d code on line 9

    Any ideas?


    You are putting the code inside php tags, correct?

    if( strpos( $post->post_title, 'computers' ) )
    		echo 'title has word computers';

    Yes, I’m doing something like this:

    <?php if( strpos( $post->post_title, 'oranges' ) )
    		echo 'title has word oranges'; { ?>
    <p>orange text here</p>
    <?php echo do_shortcode('[shortcodeorange]'); ?>
    <?php } elseif( strpos( $post->post_title, 'apples' ) )
    		echo 'title has word apples'; { ?>
    <p>apple text here</p>
    <?php echo do_shortcode('[shortcodeapple]'); ?>
    <?php } else { ?>
    <?php } ?>

    Your braces { are in the wrong place.

    if( strpos( $post->post_title, 'oranges' ) ) { ?>
      <p>orange text here</p>
      <?php echo do_shortcode('[shortcodeorange]');
    } elseif( strpos( $post->post_title, 'apples' ) ) { ?>
      <p>apple text here</p>
      <?php echo do_shortcode('[shortcodeapple]');
    } else {

    Ah, thanks again, but still no joy. If I replace if( strpos( $post->post_title, 'oranges' ) ) with if ( is_page('whole page name including oranges') ) it works. So, everything else seems to be correct, but for some reason the ( strpos( $post->post_title, 'oranges' ) ) doesn’t seem to be finding the word in the title. (?)

    Incidentally, the pages I’m trying to do this on are “virtual” pages, i.e. pages that are created by a plugin, and are not included in the database. For example, these pages can’t be found when using the normal WordPress search function. Maybe that’s why this isn’t working?

    It’s weird, though, that the is_page works. One would think that if it could recognize the whole title, it should also be able to recognize one word in that title.


    May have to use $post[0]->post_title instead of $post->post_title

    Tried that, slightly different error message this time:
    Parse error: syntax error, unexpected T_STRING in /home/user/public_html/wp-content/plugins/php-code-widget/execphp.php(44) : eval()'d code on line 2

    Also checking strpos for non-zero result is improper. This is because if your title is “orange fruit”, strpos($post->post_title, ‘orange’) will return zero which is the correct result. But your comparison will treat it as orange was not found.

    Also because the words in the title may not be of the same case, you should be doing this instead

    if (stripos($post->title, 'orange')!==false) {
      echo "orange found, output everything here without the unnecessary and potentially self-confusing breaks in php blocks",
      "<p>Orange Text here</p>",
      "orange done<p>";
    } else if (stripos($post->title, 'apple')!==false) {
      echo "Apple found, output everything in a different way again without breaking php blocks";
      echo "<p>Apple Text here</p>";
      echo do_shortcode('[shortcodeapple]');

    Although technically if you have a predetermined list of more than a couple of possible values, it’s usually better/neater to use SWITCH-CASE instead of IF-ELSE.

    OK, tried that, no error this time, but nothing else, either. Wow, I’m getting in way over my head here!

    Try adding this

    else {
      echo "No match";
    echo "Code done<br>";

    That should at least let us know if the code was executed or was there simply no match. If there was no match when there should be, then you need to var_dump/echo the variables to see what are the values being passed to your code.

    Yep, it’s saying no match. I Googled around, but I’m not sure how to place the var_dump, though. Sorry I’m so clueless abou this!

    Where is $post coming from? You can’t reference an object’s properties, such as $post->title unless you have that object available to you..

    Print out the $post variable and make sure that it is firstly available..

    Without PHP tags.

    print '<pre>';print_r( $post );print '</pre>';


    <?php print '<pre>';print_r( $post );print '</pre>'; ?>

    NOTE: All the print’s are on the same line for a reason, if you want to comment them out after testing you only need to make one adjustment…… (place // before the first print)

    Use whichever is appropriate.. if you get no output, then you’ve found the source of the problem… 😉

    NOTE: I also believe your reference might also be incorrect, shouldn’t that be $post->post_title and not $post->title?

    Try putting

    global $post;

    Right at the beginning and see if it helps.

    If not, replace echo “No Match”; with

    echo "No match<br><pre>", var_dump($post), "</pre><p>";

    This would dump all the data within the $post variable in a neat list (hence the

     tags) and let you check what is actually being used or is $post actually empty or something.

    Mark, you’re right, I get no output.

    Xephan, global $post; didn’t work, and the var_dump yielded

    No match

    So, it appears that $post->title won’t work for this, but as I stated in the first post, is_page does work, but only if I use the entire page title. So, I’m back to square one. If it can find the whole page title, seems like there’s gotta be a way to make it find a partial page title, but I have no idea how.

Viewing 15 replies - 1 through 15 (of 23 total)
  • The topic ‘Conditional Tag for any page with a particular word in the title?’ is closed to new replies.