Support » Plugins » Hacks » putting wp_title into a variable is evaluating strange

  • Resolved Jpyper


    i’m trying to change a custom query based on the page title, so I’m using wp_title('',false); to put into a variable, then use a switch statement to do different things.

    Using this code:

    $my_page_name = wp_title('',false);
    echo 'page name: '.$my_page_name.'<br />';
    switch ($my_page_name){
    	case "Activities and Family Fun":
    		$cat_slug = "activites-and-family-fun";
    	case "Automotive":
    		$cat_slug = "automotive";
    	case "Dry Cleaning":
    		$cat_slug = "dry-cleaning";
    		echo "no cat_slug value found, but the page name was ".$my_page_name."<br />";

    i actually have like 10 more cases in that statement but that’s irrelevant. $my_page_name echoes as “Automotive” so the $cat_slug should be set to “automotive”, but instead it goes to the default where $my_page_name is still “Automotive.”

    That seems completely wrong, but here’s what’s the crazy part: if I add this code before my switch statement

    if($my_page_name = "Automotive")
    		echo 'himom';
    		echo 'everything is insane';

    then it echoes “himom” and my switch statement work correctly, $cat_slug gets set to “automotive”.

    But why!?

    Is there something about switch statements that I don’t know about or is it something in the wp_title call that’s buggy, or something else? My variable has the right information, but my switch statement can’t tell unless I run an if statement with that variable first, and that doesn’t make any sense at all to me.

    Thanks for any help.

Viewing 3 replies - 1 through 3 (of 3 total)
  • esmi


    Forum Moderator

    if($my_page_name = "Automotive") is incorrect. It should be if($my_page_name == "Automotive"). The switch logic seems sound, so try using $my_page_name =trim( wp_title('',false) ); in case you’re falling foul of trailing/leading whitespace.

    ah yes, now when I use the right comparison I get “everything is insane”, which is how I am feeling.

    but the trim worked! I had wondered about whitespace since wp_title allows for tags before and after the title string, but when I printed the variable it looked like things were fine.

    THANK YOU SO MUCH! Especially for the speedy reply.



    Forum Moderator

    Glad I could help 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘putting wp_title into a variable is evaluating strange’ is closed to new replies.