Forums

[resolved] Using get_the_category() in theme's functions.php (5 posts)

  1. seg
    Member
    Posted 3 years ago #

    I am trying to use get_the_category() in my theme's functions.php file. When I print the array as seen below, I get an empty array. This code works just fine in the templates.

    $category = get_the_category();
    print_r($category);

    Is there a trick that enables the use of template tags in this file?

  2. MichaelH
    Volunteer
    Posted 3 years ago #

    This code works for me. Must be used in The Loop!!!. Test in the the index.php, using the WordPress Default theme:

    <?php my_get_cat(); ?>

    and this in the functions.php file:

    function my_get_cat() {
    $category = get_the_category();
    print_r($category);
    }
  3. seg
    Member
    Posted 3 years ago #

    Sorry for not being more clear. I am actually trying to get an array of categories in the functions.php file for use in a separate function. For some reason I can't create an array of categories in functions.php while using get_the_category(). Possibly because the SQL data or get_the_category() functions are not referenced in the functions.php file?

    Is there a SQL command or another function similar to get_the_category() that would work and accomplish this task in functions.php?

    Thanks for your help!

  4. MichaelH
    Volunteer
    Posted 3 years ago #

  5. seg
    Member
    Posted 3 years ago #

    Thank you - that is exactly what I am looking for!

Topic Closed

This topic has been closed to new replies.

About this Topic