Support » Fixing WordPress » Passing a Variable as The Property Of a Function

  • Hello and thank you for being there.

    I run into this problem sometimes. Usually I want to use the content of a variable as the property of a WP function.

    For example, this code:

    $aCategory = single_cat_title();
    $theId = get_cat_id('My Category');
    echo "Category:  " . $aCategory . " - " . $theId;

    shows this: “Category: My Category – 3”

    But this other code, which is supposed to be the same but using a variable for the property of the get_cat_id() function, shows everything except the Category ID.

    $aCategory = single_cat_title();
    $theId = get_cat_id($aCategory); //here is the only changed I did
    echo "Category:  " . $aCategory . " - " . $theId;

    It shows: “Category: My Category – 0”

    I have tried


    And both give me 0, instead of the category id.

    Any idea?

    Thanks a lot!

Viewing 1 replies (of 1 total)
  • When I run into this, it is usually because the variable does not contain exactly what I think it does. A lot of time it contains extra spaces which compress to a single space on the screen and looks legit.

    Try replacing non-printable characters with question marks in $aCategory and echoing the result:

    $test = preg_replace( '/[^[:print:]]/', '?', $aCategory );
    echo "<p>TEST:$test</p>";
Viewing 1 replies (of 1 total)
  • The topic ‘Passing a Variable as The Property Of a Function’ is closed to new replies.