• Resolved ubenzer

    (@ubenzer)


    Hello.

    I am modifiing my theme. When only showing posts that belong to a category (and subcategories of that) (using archive.php), I need to learn the category id which is shown.

    For example I have a category names “Computer”. Its id is 12.

    I am on a page https://site.com/categories/computer that shows all posts which are categorized in Computer or one of its sub categories. The output is generated in categories.php (I don’t want to use category-slug.php structure) In categories.php how can i learn Computer’s id, 12?

Viewing 3 replies - 1 through 3 (of 3 total)
  • vtxyzzy

    (@vtxyzzy)

    A post can be in several categories, so you may have a hard time choosing the correct one from an array, but here is how to get the array:

    wp_get_post_categories() will return an array of category ids for the current post. I think the array is in order of category name.

    Thread Starter ubenzer

    (@ubenzer)

    Hello. I don’t want to learn a post’s categories. I am talking about a category filter.

    I studied WP source code, and found something like this to get category id.

    $cat = intval(get_query_var('cat'));
    if (!empty($cat) && !(strtoupper($cat) == 'ALL'))

    Modified it, for self use and I am happy now. ??

    vtxyzzy

    (@vtxyzzy)

    Glad you worked it out!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to get a category id when listing posts that belong to that category’ is closed to new replies.