WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 19 months ago

#16312 closed defect (bug) (fixed)

is_category() returns false while in index

Reported by: FrancescoRizzi Owned by: nacin
Milestone: 3.5 Priority: normal
Severity: normal Version: 3.0.4
Component: Inline Docs Keywords: has-patch commit
Focuses: Cc:

Description

Let's say the incoming URL is your typical request for a top-level category, eg:
http://www.test.com/categorybase/Cat-A
Let's say Cat-A doesn't exist.
Finally, let's say the theme includes both index.php and category.php.

Then, WP calls index.php instead of (as one may expect) category.php - which may be debatable. However, what I think is a bug is that under these conditions, a call to is_category() from within index.php returns false. At least, according to the docs in the source, I would expect it to return true:

Whether current page query contains a category name or given category name.
The category list can contain category IDs, names, or category slugs.
If any of them are part of the query, then it will return true.

The URL, in fact, did include /categorybase/Cat-A

Yes, WP may not be able to tell that Cat-A is supposed to be a category because, as stated above, Cat-A does not exist...

The fix may be simply to document that is_category determines

Whether current page query contains a category name or given category name
(matching only existing categories).

Alternatively, is_category could return true for any query that looks like a requets for a category archive (possibly: any URL starting with the base prefix + category base + anything beyond that) - which may be the spirit of is-category (or not - you make the call)

I'm marking this as bug just because I don't know any better - but I can see the argument that this should be categorized as enhancement or a doc issue..

Attachments (1)

16312.patch (6.1 KB) - added by SergeyBiryukov 3 years ago.

Download all attachments as: .zip

Change History (12)

comment:1 scribu3 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Indeed, fixing the Codex is the correct thing to do here.

comment:2 scribu3 years ago

  • Component changed from General to Query
  • Keywords needs-patch added; category removed
  • Milestone set to Future Release
  • Resolution invalid deleted
  • Status changed from closed to reopened

Ah, sorry, you meant inline docs.

comment:3 in reply to: ↑ description ; follow-up: SergeyBiryukov3 years ago

Replying to FrancescoRizzi:

Then, WP calls index.php instead of (as one may expect) category.php - which may be debatable.

Actually a 404 template is used here (same for non-existing posts, pages, tags). According to Template Hierarchy, it can be index.php if 404.php doesn't exist in your theme.

If we decide to update the inline docs, I guess we also need to include that explanation
for all appropriate is_*() functions for consistency.

comment:4 follow-up: nacin3 years ago

I don't see the documentation issue here, to be honest. The category doesn't exist, so the request will 404 and the various flags will all be false, except for is_404. Same goes for pages, posts, really anything queryable.

comment:5 in reply to: ↑ 4 FrancescoRizzi3 years ago

Replying to nacin:

I don't see the documentation issue here, to be honest. The category doesn't exist, so the request will 404 and the various flags will all be false, except for is_404. Same goes for pages, posts, really anything queryable.

I think the issue is that the inline doc don't hint at the actual behaviour in this scenario

Whether current page query contains a category name or given category name.

Is not the same as

Whether the current page query contains a category name, or given category name, and (if so) whether the category exists.

comment:6 in reply to: ↑ 3 FrancescoRizzi3 years ago

Replying to SergeyBiryukov:

Replying to FrancescoRizzi:

Then, WP calls index.php instead of (as one may expect) category.php - which may be debatable.

Actually a 404 template is used here (same for non-existing posts, pages, tags). According to Template Hierarchy, it can be index.php if 404.php doesn't exist in your theme.

That makes sense - thanks for explaining that bit of mystery to me. And to confirm: I did not have a 404.php template in my scenario.

If we decide to update the inline docs, I guess we also need to include that explanation
for all appropriate is_*() functions for consistency.

Agreed (at least, where applicable)

comment:7 SergeyBiryukov3 years ago

There's a different description in current trunk:

Is the query for a category archive page?

This can probably be:

Is the query for an existing category archive page?

SergeyBiryukov3 years ago

comment:8 SergeyBiryukov3 years ago

  • Keywords has-patch added; needs-patch removed

Added patch for all descriptions where it seems appropriate.

comment:9 SergeyBiryukov22 months ago

  • Component changed from Query to Inline Docs

comment:10 nacin19 months ago

  • Keywords commit added
  • Milestone changed from Future Release to 3.5

comment:11 nacin19 months ago

  • Owner set to nacin
  • Resolution set to fixed
  • Status changed from reopened to closed

In [21890]:

Clarify that conditional tags only return true for existing objects, not 404 situations. props SergeyBiryukov, fixes #16312.

Note: See TracTickets for help on using tickets.