Support » Themes and Templates » Strange output for get_cat_id()

  • Resolved rwwood

    (@rwwood)


    I’m working on a template for which I’m trying to get category IDs, and in testing, I put the following piece of code in the template:

    <?php
            $cat_title = single_cat_title();
            $category_id = get_cat_id($cat_title);
            echo $category_id;
            ?>

    What gets returned is, for example, Booklets(), where Booklets is the single_cat_title. If I echo $cat_title, I get the expected result: Booklets.

    I am admittedly not very proficient at php, so I can’t see where the problem is.

    Help appreciated.

Viewing 8 replies - 1 through 8 (of 8 total)
  • function would be get_cat_ID. Try again with this.

    Same result. I misread the output in my original post, though. What gets returned is Booklets0, not Booklets ()

    Try this code:

    <?php
            $cat_title = single_cat_title();
            $category_id = get_cat_id(strtolower($cat_title));
            echo $category_id;
    ?>

    Nope. That still returns Booklets0. If I use the actual category name, like

    <?php
            $category_id = get_cat_ID( 'Booklets');
            echo $category_id;
    ?>

    I get the right return: 4 which is the cat ID for Booklets. That would be fine if I was trying to build a template for that one category, but I want it to be independent of which category is being viewed.

    I’m afraid that I’ve been reading that for quite some time, but nothing I’ve read resolves the problem I’m having. What are you referring to?

    to use the single cat title in a string, as you are planning, you need to use this code:

    <?php $current_category = single_cat_title("", false); ?>

    (the ‘false’ is the important part)
    this line is directly from the linked docu.

    for your code, it would be obviously:
    $cat_title = single_cat_title('', false);

    Duh. I read that article several times and tried to implement “false”, but didn’t have the syntax right. I totally missed the example given.

    Thanks! That fixed it.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Strange output for get_cat_id()’ is closed to new replies.