• Resolved thepaperdump

    (@thepaperdump)


    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.

Viewing 13 replies - 1 through 13 (of 13 total)
  • 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…

    Thread Starter thepaperdump

    (@thepaperdump)

    You rock.

    Thread Starter thepaperdump

    (@thepaperdump)

    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;
    }

    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

    ojster

    (@ojster)

    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!

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

    Thread Starter thepaperdump

    (@thepaperdump)

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

    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

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    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.

    @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

    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

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

    get_query_var(‘cat’)

    is OK.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Category ID from current URL nicename’ is closed to new replies.