WordPress.org

Forums

[resolved] Strange output for get_cat_id() (9 posts)

  1. rwwood
    Member
    Posted 4 years ago #

    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.

  2. chinmoy29
    Member
    Posted 4 years ago #

    function would be get_cat_ID. Try again with this.

  3. rwwood
    Member
    Posted 4 years ago #

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

  4. chinmoy29
    Member
    Posted 4 years ago #

    Try this code:

    <?php
            $cat_title = single_cat_title();
            $category_id = get_cat_id(strtolower($cat_title));
            echo $category_id;
    ?>
  5. rwwood
    Member
    Posted 4 years ago #

    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.

  6. rwwood
    Member
    Posted 4 years ago #

    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?

  7. 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);

  8. rwwood
    Member
    Posted 4 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.