Make WordPress Core

Opened 14 years ago

Closed 13 years ago

#15442 closed defect (bug) (fixed)

Indexes no longer start at 0 for get_the_category()

Reported by: ramenboy's profile ramenboy Owned by:
Milestone: 3.1 Priority: normal
Severity: normal Version: 3.1
Component: General Keywords: has-patch
Focuses: Cc:


Due to changes related to #15407, it seems that the keys in the array returned by get_the_category() are now category IDs instead of being 0-based. As a result, code such as the following now breaks:

$category = get_the_category();
echo $category[0]->cat_name;

The above usage is from the docs:

Attachments (2)

15442.diff (489 bytes) - added by greuben 13 years ago.
15442-2.diff (481 bytes) - added by greuben 13 years ago.
based on IRC discussion

Download all attachments as: .zip

Change History (12)

#1 @filosofo
14 years ago

Don't you think it would be better just to change the docs?

#2 @nacin
14 years ago

Unfortunately I am betting this is common usage.

#3 @gazouteast
13 years ago

  • Cc gazouteast added

Possibly related, but certainly a side effect

Multiple issues surfacing -

$category = get_the_category(); = fails to get the category ID numbers
echo $category[0]->cat_name; = (tested on two different servers now) the " -> " is being read as the closer for a " <!-- " HTML comment tag.

The context I'm trying to use this in, is in the sidebar, in a widget, to grab the category ID number and assign it to a variable for use in a function call. In this instance, it's a new build where the category number, Adrotate group and block, and the NextGEN gallery ID numbers are all synchronised such that ID 1 = name Art in all cases. But, the category ID call is failing and blocking the build from progressing.

I'm not submitting this as a support shout, but rather to give you an instance of failure. The initial ticket report now gives me a clue as to why.

#4 @nacin
13 years ago

  • Milestone changed from Awaiting Review to 3.1

13 years ago


#5 @greuben
13 years ago

  • Keywords needs-testing added

#6 @nacin
13 years ago

In the wild: #15776

13 years ago

based on IRC discussion

#7 @scribu
13 years ago

If we do decide to use array_values(), maybe it should be called further down, in get_the_terms() ?

#8 @nacin
13 years ago

I imagine that would then break backwards compatibility for those using get_the_terms() and expecting term ids as indicies.

#9 @scribu
13 years ago

  • Keywords has-patch added; needs-testing removed

Right, nevermind then.

#10 @scribu
13 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [16888]) Re-index categories in get_the_category(). Props greuben. Fixes #15442

Note: See TracTickets for help on using tickets.