• ebonmuse

    (@ebonmuse)


    Hello all,

    I’ve been searching, unsuccessfully, for some sort of function that can be called in a category page and that returns the numeric ID corresponding to that category. After a great deal of brain-wracking, I finally resorted to modifying wp-includes/functions.php by adding the following function, which I based on is_category:

    function fetch_category_id() {
    global $wp_query;

    if ( !$wp_query->is_category )
    return false;

    $cat_obj = $wp_query->get_queried_object();
    return $cat_obj->cat_ID;
    }

    This does just what I need, but it seems so simple in retrospect that it’s hard to believe WP doesn’t already have something like it. Have I reinvented the wheel?

Viewing 4 replies - 1 through 4 (of 4 total)
  • moshu

    (@moshu)

    I have to warn you I am not a coder… but isn’t this Template_Tags/get_the_category doing something similar?
    (as I said, I might be wrong)

    Thread Starter ebonmuse

    (@ebonmuse)

    Not exactly. That function returns a list of the categories associated with a specific post; what I want is something associated with a general category page. For example, if my categories are 3: “cooking”, 4: “reading” and 5: “miscellaneous”, and I’m on the page “/category/cooking”, I want a function I can call on the template for that page that returns “3”.

    Kafkaesqui

    (@kafkaesqui)

    Try this:

    $this_cat = get_query_var('cat');

    $this_cat should now hold the value of the category’s ID.

    Is

    $this_cat = get_query_var('cat');

    all you need? for example, if I want to show a dropdown archive menu, I could use this:
    <?php dropdown_cats(); ?>

    could you somehow combine both to get a dropdown archive menu of JUST the current category? as:

    <?php dropdown_cats($this_cat); ?>

    ?? just tried it, but I guess not. Or I don’t know how to put it together. It seems the dropdown_cats just does one thing… hmmm, how to build a dropdown menu or even a list of JUST the titles from the current category?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Getting the ID of a category page’ is closed to new replies.