WordPress.org

Ready to get started?Download WordPress

Forums

Conditional Tag for any page with a particular word in the title? (24 posts)

  1. Mike
    Member
    Posted 3 years ago #

    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?

    thanks,
    Mike

  2. MichaelH
    Member
    Posted 3 years ago #

    if( strpos( $post->post_title, 'computers' ) )
    		echo 'title has word computers';
  3. Mike
    Member
    Posted 3 years ago #

    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?

    thanks,
    Mike

  4. MichaelH
    Member
    Posted 3 years ago #

    You are putting the code inside php tags, correct?

    <?php
    if( strpos( $post->post_title, 'computers' ) )
    		echo 'title has word computers';
    ?>
  5. Mike
    Member
    Posted 3 years ago #

    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 } ?>
  6. MichaelH
    Member
    Posted 3 years ago #

    Your braces { are in the wrong place.

    <?php
    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 {
    }
    ?>
  7. Mike
    Member
    Posted 3 years ago #

    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.

    Thoughts?

  8. MichaelH
    Member
    Posted 3 years ago #

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

  9. Mike
    Member
    Posted 3 years ago #

    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

  10. Xephan
    Member
    Posted 3 years ago #

    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>",
      do_shortcode('[shortcodeorange]'),
      "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.

  11. Mike
    Member
    Posted 3 years ago #

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

  12. Xephan
    Member
    Posted 3 years ago #

    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.

  13. Mike
    Member
    Posted 3 years ago #

    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!

  14. Mark / t31os
    Moderator
    Posted 3 years ago #

    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>';

    With.

    <?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?

  15. Xephan
    Member
    Posted 3 years ago #

    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.
  16. Mike
    Member
    Posted 3 years ago #

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

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

    No match
    NULL

    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.

  17. Xephan
    Member
    Posted 3 years ago #

    Most likely because you're generating this all from within your plugin, the usual WordPress globals like $page are not populated at all.

    In which case, you will probably have to resort to running a query to pull the relevant titles and compare.

    Without more code, it's not possible to give more exact help.

  18. Mark / t31os
    Moderator
    Posted 3 years ago #

    Explain what it is you're trying to detect and do, then i think we might be able to offer more definitive advice/suggestions.. please..

    Are you simply trying to detect (or conditionally run code) when your plugin page is viewed? (there's hooks for that)...

    Please clarify... :)

  19. Mike
    Member
    Posted 3 years ago #

    The plugin generates pages for real estate property listings from our local MLS. So for any given listing, the property address is the page title, say: 123 Main Street, Anytown USA, 12345. I'd like to put some code in the sidebar that looks at the zip code in the title and plugs that into a shortcode to display other nearby listings in the sidebar.

  20. Mark / t31os
    Moderator
    Posted 3 years ago #

    What plugin are you referring to? I'm trying to grasp what these pages actually are, you mentioned them not existing in the tradional sense (maybe they're just excluded, but still actual pages - are they listed under Pages in the admin area?)...

  21. Mike
    Member
    Posted 3 years ago #

    The plugin is dsIDXpress. No, the pages are not in the admin area. Here's a sample listing page on the demo site: http://www.daniellecordova.com/idx/mls-p756492-52_segada_rancho_santa_margarita_ca_92688

  22. Mark / t31os
    Moderator
    Posted 3 years ago #

    I'd suggest asking for support from the authors, i'd have thought that comes with the cost of using the service?.. have you tried contacting them via their contact form?

  23. Mike
    Member
    Posted 3 years ago #

    Yeah, the authors are great and supportive, I'll give them a shout if need be. Thanks for all the help here. The WordPress community is awesome!

  24. Joe Greenwood
    Member
    Posted 3 years ago #

    Xephan, Mark, Mike, thank you, this works for me:

    <?php
    global $post;
    if (stripos($post->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>",
      do_shortcode('[shortcodeorange]'),
      "orange done<p>";
    } else if (stripos($post->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]');
    }
    
    else {
      echo "No match";
    }
    
    echo "Code done<br>";
     ?>

Topic Closed

This topic has been closed to new replies.

About this Topic