WordPress.org

Ready to get started?Download WordPress

Forums

Passing a Variable as The Property Of a Function (2 posts)

  1. teauser
    Member
    Posted 3 years ago #

    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

    get_cat_id($myCategory);
    get_cat_id('$myCategory');

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

    Any idea?

    Thanks a lot!

  2. vtxyzzy
    Member
    Posted 3 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic