Forums

[resolved] How to get a category id when listing posts that belong to that category (4 posts)

  1. UBenzer
    Member
    Posted 2 years ago #

    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 http://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?

  2. vtxyzzy
    Member
    Posted 2 years ago #

    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.

  3. UBenzer
    Member
    Posted 2 years ago #

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

  4. vtxyzzy
    Member
    Posted 2 years ago #

    Glad you worked it out!

Topic Closed

This topic has been closed to new replies.

About this Topic