[wp-trac] [WordPress Trac] #22110: Check for current conditional
WordPress Trac
wp-trac at lists.automattic.com
Fri Oct 5 18:09:00 UTC 2012
#22110: Check for current conditional
-------------------------+------------------------------
Reporter: mintindeed | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: General | Version: trunk
Severity: normal | Resolution:
Keywords: |
-------------------------+------------------------------
Comment (by mintindeed):
Replying to [comment:3 nacin]:
> Off the top of my head, I can think of four supersets of other flags:
is_time, is_date, is_archive, is_singular. So, is_time is true when
is_hour, is_minute, or is_second is true. is_date is true when is_day,
is_month, or is_year. is_singular when is_single or is_page. (Also
is_attachment, but when is_attachment is true, is_single XOR is_page is
always true.) is_archive is a superset of a whole bunch of conditionals,
such as is_tag, is_category, is_tax, is_post_type_archive, etc.
>
> I would probably just code around those supersets. It can probably be
much saner than a giant if/elseif/elseif/... statement.
If I weren't trying to make it "everything for everybody" it would look
more like this:
{{{
function where_am_i() {
global $wp_query;
if ( ! isset( $wp_query ) ) {
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags
do not work before the query is run. Before then, they always return
false.' ), '3.5' );
return false;
}
// Tried to maintain the hierarchy from query.php
if ( true === $wp_query->is_robots ) {
return 'robots';
} elseif ( true === $wp_query->is_attachment ) {
return 'attachment';
} elseif ( true === $wp_query->is_page ) {
return 'page';
} elseif ( true === $wp_query->is_single ) {
return 'single';
} elseif ( true === $wp_query->is_search ) {
return 'search';
} elseif ( true === $wp_query->is_time ) {
return 'time';
} elseif ( true === $wp_query->is_date ) {
return 'date';
} elseif ( true === $wp_query->is_category ) {
return 'category';
} elseif ( true === $wp_query->is_tag ) {
return 'tag';
} elseif ( true === $wp_query->is_tax ) {
return 'custom_taxonomy';
} elseif ( true === $wp_query->is_author ) {
return 'author';
} elseif ( true === $wp_query->is_post_type_archive ) {
return 'post_type';
} elseif ( true === $wp_query->is_feed ) {
return 'feed';
} elseif ( true === $wp_query->is_trackback ) {
return 'trackback';
} elseif ( true === $wp_query->is_admin ) {
return 'admin';
} elseif ( true === $wp_query->is_404 ) {
return '404';
} elseif ( true === $wp_query->is_home ) {
return 'home';
}
return null;
}
}}}
--
Ticket URL: <http://core.trac.wordpress.org/ticket/22110#comment:6>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list