WordPress.org

Ready to get started?Download WordPress

Forums

Help me display the ID of the category in the functions.php file (6 posts)

  1. hetbuon
    Member
    Posted 1 year ago #

    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 ]

  2. WPyogi
    Volunteer Moderator
    Posted 1 year ago #

  3. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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

  4. hetbuon
    Member
    Posted 1 year ago #

    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

  5. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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?

  6. hetbuon
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.