WordPress.org

Ready to get started?Download WordPress

Forums

Pull current category ID information (5 posts)

  1. mylagoon
    Member
    Posted 8 years ago #

    Hi,

    I need to be able to pull the current category's ID, and that ID only.

    I've tried the following piece of code
    <?php foreach((get_the_category()) as $cat) { echo $cat->cat_ID . ' '; } ?>

    Whilst that does pull the current categories ID, it also appears to be pulling the last sub-category for the category's ID.

    For example, Category 11 has a sub-category, Category 12.
    So when I'm on category 11s page, the above query spits out the following: 11%2012.

    I need it to only spit out 11 when I'm on parent category 11s page, and only spit out 12 when I'm on sub-category 12s page.

    Any ideas anyone?

  2. Joshua Sigar
    Member
    Posted 8 years ago #

    Well, then don't loop thru the cat IDs, just spit out the first one.

  3. mylagoon
    Member
    Posted 8 years ago #

    The first one it spits out is the incorrect Cat ID when you are on a sub-category page.

  4. Joshua Sigar
    Member
    Posted 8 years ago #

    $current_page_cat = $wp_query->get_query_var('cat');

  5. mylagoon
    Member
    Posted 8 years ago #

    I've entered the above as follows:
    <?php $current_page_cat = $wp_query->get_query_var('cat'); ?>
    But all it seems to do is stop the rest of the coding for the page below it from loading.

    I've probably done something really simple wrong yeah?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags