WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Category ID from current URL nicename (14 posts)

  1. thepaperdump
    Member
    Posted 5 years ago #

    I'm trying to do the following:

    When the user is browsing a category page, I want to get the current category ID. This is what I'm doing:

    if(is_category() || is_single()){
     foreach(get_the_category() as $category)
     {
      $current =$category->cat_ID;
      $current_name = $category->cat_name;
     }
    }
    
    echo $current_name . " has id ".$current;

    The problem is that when I'm on a category page, get_the_category generates multiple categories, and it's getting the last category output by the function.

    I just want the category ID of the category that is currently displayed in the URL.

  2. tomgf
    Member
    Posted 5 years ago #

    Using the code that you've posted, you'll have the last category on the array (if you use $current_name .= $category->cat_name you'll have a concatenation of these values).

    What about using the function get_category_by_path()?

    Just my two cents…

  3. thepaperdump
    Member
    Posted 5 years ago #

    You rock.

  4. thepaperdump
    Member
    Posted 5 years ago #

    FYI:

    if(is_category() || is_single()){
      $cat = get_category_by_path(get_query_var('category_name'),false);
      $current = $cat->cat_ID;
      $current_name = $cat->cat_name;
    
    echo "Current category ID is ".$current." and is called ".$current_name;
    }
  5. Top7up
    Member
    Posted 5 years ago #

    Salaam everybody,
    This code is a bit smarter:

    if(is_category() || is_single()){
     $category = end(get_the_category());
     $current =$category->cat_ID;
     $current_name = $category->cat_name;
    }
    echo $current_name . " has id ".$current;

    Cheers,
    Aziz Oraij

  6. Ojster
    Member
    Posted 5 years ago #

    Well I have to say that the code from thepaperdump works correctly but the one from Top7up not. When in parent category it still displays the child.

    Just FYI and thanks to thepaperdump!

  7. joshpodolske
    Member
    Posted 5 years ago #

    where do you put this code at? header.php?

  8. thepaperdump
    Member
    Posted 5 years ago #

    yep, header.php :) or you can put it in a function and call the function in your header, loop, wherever.

  9. wbl
    Member
    Posted 5 years ago #

    Hi

    I have tried the code of thepaperdump here newarticledirectory.com/techresourceguide/?cat=1

    its not working.

    I have added the code in header, index.php but no luck.

    and also tried newarticledirectory.com/ still no luck

    I need to know the ID of a category when it is clicked. pls help

  10. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    There's a far simpler way.

    if (is_category()) {
    global $wp_query;
    $cat_obj = $wp_query->get_queried_object();
    echo $cat_obj->name . " has id ". $cat_obj->term_id;
    }

    get_queried_object returns the main object that was queried for.

  11. wbl
    Member
    Posted 5 years ago #

    @Otto42 thanks a lot.

    I have added the code in the index.php ,here newarticledirectory.com .

    I m sorry it not working yet. I will highly appreciate your help

    Thanks

  12. Skyppyno
    Member
    Posted 5 years ago #

    Great job Otto42 ... I put it in my sidebar ( not widgettized ) and it works very well.

    This is my use of this :

    I added depth=1 in wp_list_categories , so I have a Cat menu without children cat.
    Then I added :

    if(is_category()|| is_single()){global $wp_query;
    $cat_obj = $wp_query->get_queried_object();
    $current_name = $cat_obj->name ;
    $current = $cat_obj->term_id;
    ?><h4><?php echo $current_name . "Has ID =".$current;
    ?></h4><?php echo "and has subcategories as follows :";
    wp_list_categories('orderby=name&show_count=1&title_li=&use_desc_for_title=1&child_of='. $current);}

    That show me the subcategories of current cat ....

    Great !

    Thanks

    Skyppyno

  13. wbl
    Member
    Posted 5 years ago #

    It works for me when I post the code on sidebar but when I put them on index.php , it not working.
    any idea

  14. gaoxuewei
    Member
    Posted 5 years ago #

    get_query_var('cat')

    is OK.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags