WordPress.org

Support

Support » How-To and Troubleshooting » Help me display the ID of the category in the functions.php file

Help me display the ID of the category in the functions.php file

  • Hello.
    In the functions.php file I need to retrieve the id of a cateory however I have used many different functions but it still does not work.
    The function I use

     foreach (get_the_category () as $ category)
    {
    $ id = $ category-> cat_ID;}

    I also tried to use
    ¬†$ id = get_query_var (‘cat’);

    However all is not working in functions.php file
    If you put it in file category.php, it worked.
    But what I need it to work in the functions.php file
    Is there any way to make it work without you.
    Thank all

    [ do not bump – it is not permitted on these forums ]

Viewing 5 replies - 1 through 5 (of 5 total)
  • WPyogi

    @wpyogi

    Forum Moderator

    Moderator Michael

    @alchymyth

    Forum Moderator

    retrieve the id of a cateory

    what category?

    – the category of a category archive page?
    – a post’s category (which your code was trying to do)?

    both snippets below should work in the respective circumstances:

    global $post; foreach(get_the_category($post->ID) as $category)
    {
    $id = $category->cat_ID; }
    
    $id = get_query_var('cat');

    there is also:
    http://codex.wordpress.org/Function_Reference/get_category_by_slug
    and
    http://codex.wordpress.org/Function_Reference/get_term_by

    Not your game.I put code like this in your functions.php file

    function ajax_post(){
    global $post;
     foreach(get_the_category($post->ID) as $category)
    {
    $id = get_query_var('cat'); }
    			$args = array(
    				'cat' => $id,
    				'paged' => $_GET['pag']
    			);
    		}else if(isset($_GET['pag'])){
    			$args = array(
    				'paged' => $_GET['pag']
    			);
    		}

    However, the $ id variable is still not able to display id of a category.
    What do I do now.
    hepl

    Moderator Michael

    @alchymyth

    Forum Moderator

    foreach(get_the_category($post->ID) as $category)
    {
    $id = get_query_var('cat'); }

    the above section does make no sense.

    either, in a category archive, you could use:

    $id = get_query_var('cat'); }

    or, if the function is called from the loop, you could use:

    foreach(get_the_category($post->ID) as $category)
    {
    $id = $category->term_id; }

    (although this would get you only the ID of the last of possibly many post’s categories)

    the $id variable is still not able to display id of a category

    your function is not printing or returning anything;

    where are you calling the function, and what are trying to do?

    Oh.First thank you for your feedback for me but I have tried both of you.However, if the statement is placed in file category.php, it works even if the functions.php file it looks like it is disabled.

    Sort of my idea.
    When I go to a category of my site eg
    http://hetbuon.com/category/hai-huoc-facebook
    Category.php file will send a variable is the id of the category to the functions.php file.
    You how to do so.
    I’m from Vietnam and I look forward to any help ….

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Help me display the ID of the category in the functions.php file’ is closed to new replies.