1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Wordpress - "php if in_title"

Discussion in 'PHP & Perl' started by egomOnia, Mar 8, 2010.

  1. egomOnia

    egomOnia Registered Member

    Joined:
    Oct 21, 2009
    Messages:
    92
    Likes Received:
    62
    Is there some way to check if there's a certain keyword in a wordpress post title?

    According to the wordpress codex, there are functions like is_category, in_category or has_tags to check if a post is in a certain category or uses specific tags.

    But I need something like an "in_title" function to check if there is a certain keyword in the post title in order to display highly relevant ads.

    Does anyone know a way to do this?
     
  2. stoned raider

    stoned raider Registered Member

    Joined:
    Aug 4, 2008
    Messages:
    94
    Likes Received:
    22
    you'll have to play a bit with wp_title and PHP's strpos

    Code:
    http://codex.wordpress.org/Template_Tags/wp_title
    Code:
    http://www.php.net/manual/en/function.strpos.php
    assembly required
     
    • Thanks Thanks x 1
  3. yeahright

    yeahright Registered Member

    Joined:
    Jan 17, 2009
    Messages:
    93
    Likes Received:
    32
    This works, although I'm not the most advanced coder so there may be a more efficient way of scripting this:

    PHP:
    <?php

    $title 
    get_the_title();

    $keyworda 'ppc';
    $keywordb 'ppv';
    $keywordc 'cpa';

    if(
    stristr($title,$keyworda)){

     
    //show banner for ppc?

    }
    elseif(
    stristr($title,$keywordb)){

     
    //show banner for ppv?

    }
    elseif(
    stristr($title,$keywordc)){

     
    //show banner for cpa?

    }
    else {

     
    //show a generic banner

    }
    ?>
    This code must be place within the loop.
     
    • Thanks Thanks x 2
  4. egomOnia

    egomOnia Registered Member

    Joined:
    Oct 21, 2009
    Messages:
    92
    Likes Received:
    62
    I'm going to test it immediately and report the results. Thanks in advance for your efforts!
     
  5. egomOnia

    egomOnia Registered Member

    Joined:
    Oct 21, 2009
    Messages:
    92
    Likes Received:
    62
    Thanks, works exactly the way I wanted it to work!