WordPress.org

Forums

get_the_category() returns category when page is in no category (3 posts)

  1. here
    Member
    Posted 3 years ago #

    I am calling get_the_category() on a page that is not in any category, and it returns the category with term_id=1 . It should return an empty array or false or something other than an incorrect category. This seems like a bug.

    see below for excerpts from print_r($wp_query)
    this all takes place inside of a loop on a page, which seems to pull the correct page id and other attributes inside of $wp_query.

    print_r(get_the_category()) returns the following

    Array
    (
    [0] => stdClass Object
    (
    [term_id] => 1
    [name] => ...
    [slug] => ...
    [term_group] => 0
    [term_taxonomy_id] => 1
    [taxonomy] => category
    [description] => ...
    [parent] => 3
    [count] => 41
    [object_id] => 4671
    [cat_ID] => 1
    [category_count] => 41
    ...

    print_r($wp_query) returns some of this ...

    WP_Query Object
    (
    [query_vars] => Array
    (
    [page] => 0
    [pagename] => terms
    ...
    [category_name] =>
    ...
    [category__in] => Array
    (
    )
    ...
    [is_page] => 1
    ...

  2. the function should be called inside the loop; http://codex.wordpress.org/Function_Reference/get_the_category

    what is the context in your template?

    what is the purpose to call get_the_category() for a page?

  3. here
    Member
    Posted 3 years ago #

    The function is called inside the loop, as I state above.

    I am calling get_the_category() in the header to set a javascript variable which is passed to an ad server for targeting based on category. This code is executed on all pages of all type on the site.

    I can add a conditional to make this an exception, but I still think calling get_the_category() should return false or empty if there is no category for some reason, and certainly not return term_id 1 when it find an unexpected situation.

Topic Closed

This topic has been closed to new replies.

About this Topic