WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] putting wp_title into a variable is evaluating strange (4 posts)

  1. Jpyper
    Member
    Posted 2 years ago #

    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";
    		break;
    	case "Automotive":
    		$cat_slug = "automotive";
    		break;
    	case "Dry Cleaning":
    		$cat_slug = "dry-cleaning";
    		break;
    	default:
    		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';
    	else
    		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.

  2. esmi
    Forum Moderator
    Posted 2 years ago #

    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.

  3. Jpyper
    Member
    Posted 2 years ago #

    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.

  4. esmi
    Forum Moderator
    Posted 2 years ago #

    Glad I could help :-)

Topic Closed

This topic has been closed to new replies.

About this Topic