Make WordPress Core


Ignore:
Timestamp:
08/25/2010 06:05:33 PM (16 years ago)
Author:
ryan
Message:

is_* WP_Query methods. Props mdawaffe. fixes #14494

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/query.php

    r15501 r15531  
    9999
    100100/**
    101  * Is query requesting an archive page.
    102  *
     101 * Is the query for an archive page?
     102 *
     103 * Month, Year, Category, Author, ...
     104 *
     105 * @see WP_Query::is_archive()
    103106 * @since 1.5.0
    104107 * @uses $wp_query
    105108 *
    106  * @return bool True if page is archive.
     109 * @return bool
    107110 */
    108111function is_archive() {
    109112        global $wp_query;
    110113
    111         return $wp_query->is_archive;
    112 }
    113 
    114 /**
    115  * Is query requesting an attachment page.
    116  *
     114        return $wp_query->is_archive();
     115}
     116
     117/**
     118 * Is the query for an attachment page?
     119 *
     120 * @see WP_Query::is_attachment()
    117121 * @since 2.0.0
    118122 * @uses $wp_query
    119123 *
    120  * @return bool True if page is attachment.
     124 * @return bool
    121125 */
    122126function is_attachment() {
    123127        global $wp_query;
    124128
    125         return $wp_query->is_attachment;
    126 }
    127 
    128 /**
    129  * Is query requesting an author page.
    130  *
    131  * If the $author parameter is specified then the check will be expanded to
    132  * include whether the queried author matches the one given in the parameter.
    133  * You can match against integers and against strings.
    134  *
    135  * If matching against an integer, the ID should be used of the author for the
    136  * test. If the $author is an ID and matches the author page user ID, then
    137  * 'true' will be returned.
    138  *
    139  * If matching against strings, then the test will be matched against both the
    140  * nickname and user nicename and will return true on success.
    141  *
     129        return $wp_query->is_attachment();
     130}
     131
     132/**
     133 * Is the query for an author archive page?
     134 *
     135 * If the $author parameter is specified, this function will additionally
     136 * check if the query is for one of the authors specified.
     137 *
     138 * @see WP_Query::is_author()
    142139 * @since 1.5.0
    143140 * @uses $wp_query
    144141 *
    145  * @param string|int $author Optional. Is current page this author.
    146  * @return bool True if page is author or $author (if set).
    147  */
    148 function is_author($author = '') {
    149         global $wp_query;
    150 
    151         if ( !$wp_query->is_author )
    152                 return false;
    153 
    154         if ( empty($author) )
    155                 return true;
    156 
    157         $author_obj = $wp_query->get_queried_object();
    158 
    159         $author = (array) $author;
    160 
    161         if ( in_array( $author_obj->ID, $author ) )
    162                 return true;
    163         elseif ( in_array( $author_obj->nickname, $author ) )
    164                 return true;
    165         elseif ( in_array( $author_obj->user_nicename, $author ) )
    166                 return true;
    167 
    168         return false;
    169 }
    170 
    171 /**
    172  * Whether current page query contains a category name or given category name.
    173  *
    174  * The category list can contain category IDs, names, or category slugs. If any
    175  * of them are part of the query, then it will return true.
    176  *
     142 * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
     143 * @return bool
     144 */
     145function is_author( $author = '' ) {
     146        global $wp_query;
     147
     148        return $wp_query->is_author( $author );
     149}
     150
     151/**
     152 * Is the query for a category archive page?
     153 *
     154 * If the $category parameter is specified, this function will additionally
     155 * check if the query is for one of the categories specified.
     156 *
     157 * @see WP_Query::is_category()
    177158 * @since 1.5.0
    178159 * @uses $wp_query
    179160 *
    180  * @param string|array $category Optional.
     161 * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
    181162 * @return bool
    182163 */
    183 function is_category($category = '') {
    184         global $wp_query;
    185 
    186         if ( !$wp_query->is_category )
    187                 return false;
    188 
    189         if ( empty($category) )
    190                 return true;
    191 
    192         $cat_obj = $wp_query->get_queried_object();
    193 
    194         $category = (array) $category;
    195 
    196         if ( in_array( $cat_obj->term_id, $category ) )
    197                 return true;
    198         elseif ( in_array( $cat_obj->name, $category ) )
    199                 return true;
    200         elseif ( in_array( $cat_obj->slug, $category ) )
    201                 return true;
    202 
    203         return false;
    204 }
    205 
    206 /**
    207  * Whether the current page query has the given tag slug or contains tag.
    208  *
     164function is_category( $category = '' ) {
     165        global $wp_query;
     166
     167        return $wp_query->is_category( $category );
     168}
     169
     170/**
     171 * Is the query for a tag archive page?
     172 *
     173 * If the $tag parameter is specified, this function will additionally
     174 * check if the query is for one of the tags specified.
     175 *
     176 * @see WP_Query::is_tag()
    209177 * @since 2.3.0
    210178 * @uses $wp_query
    211179 *
    212  * @param string|array $slug Optional. Single tag or list of tags to check for.
     180 * @param mixed $slug Optional. Tag slug or array of slugs.
    213181 * @return bool
    214182 */
     
    216184        global $wp_query;
    217185
    218         if ( !$wp_query->is_tag )
    219                 return false;
    220 
    221         if ( empty( $slug ) )
    222                 return true;
    223 
    224         $tag_obj = $wp_query->get_queried_object();
    225 
    226         $slug = (array) $slug;
    227 
    228         if ( in_array( $tag_obj->slug, $slug ) )
    229                 return true;
    230 
    231         return false;
    232 }
    233 
    234 /**
    235  * Whether the current query is for the given taxonomy and/or term.
    236  *
    237  * If no taxonomy argument is set, returns true if any taxonomy is queried.
    238  * If the taxonomy argument is passed but no term argument, returns true
    239  *    if the taxonomy or taxonomies in the argument are being queried.
    240  * If both taxonomy and term arguments are passed, returns true
    241  *    if the current query is for a term contained in the terms argument
    242  *    which has a taxonomy contained in the taxonomy argument.
    243  *
     186        return $wp_query->is_tag( $slug );
     187}
     188
     189/**
     190 * Is the query for a taxonomy archive page?
     191 *
     192 * If the $taxonomy parameter is specified, this function will additionally
     193 * check if the query is for that specific $taxonomy.
     194 *
     195 * If the $term parameter is specified in addition to the $taxonomy parameter,
     196 * this function will additionally check if the query is for one of the terms
     197 * specified.
     198 *
     199 * @see WP_Query::is_tax()
    244200 * @since 2.5.0
    245201 * @uses $wp_query
    246202 *
    247  * @param string|array $taxonomy Optional. Taxonomy slug or slugs to check in current query.
    248  * @param int|array|string $term. Optional. A single or array of, The term's ID, Name or Slug
     203 * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
     204 * @param mixed $term. Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
    249205 * @return bool
    250206 */
     
    252208        global $wp_query, $wp_taxonomies;
    253209
    254         $queried_object = $wp_query->get_queried_object();
    255         $tax_array = array_intersect(array_keys($wp_taxonomies), (array) $taxonomy);
    256         $term_array = (array) $term;
    257 
    258         if ( !$wp_query->is_tax )
    259                 return false;
    260 
    261         if ( empty( $taxonomy ) )
    262                 return true;
    263 
    264         if ( empty( $term ) ) // Only a Taxonomy provided
    265                 return isset($queried_object->taxonomy) && count( $tax_array ) && in_array($queried_object->taxonomy, $tax_array);
    266 
    267         return isset($queried_object->term_id) &&
    268                         count(array_intersect(
    269                                 array($queried_object->term_id, $queried_object->name, $queried_object->slug),
    270                                 $term_array
    271                         ));
     210        return $wp_query->is_tax( $taxonomy, $term );
    272211}
    273212
     
    275214 * Whether the current URL is within the comments popup window.
    276215 *
     216 * @see WP_Query::is_comments_popup()
    277217 * @since 1.5.0
    278218 * @uses $wp_query
     
    283223        global $wp_query;
    284224
    285         return $wp_query->is_comments_popup;
    286 }
    287 
    288 /**
    289  * Whether current URL is based on a date.
    290  *
     225        return $wp_query->is_comments_popup();
     226}
     227
     228/**
     229 * Is the query for a date archive?
     230 *
     231 * @see WP_Query::is_date()
    291232 * @since 1.5.0
    292233 * @uses $wp_query
     
    297238        global $wp_query;
    298239
    299         return $wp_query->is_date;
    300 }
    301 
    302 /**
    303  * Whether current blog URL contains a day.
    304  *
     240        return $wp_query->is_date();
     241}
     242
     243/**
     244 * Is the query for a day archive?
     245 *
     246 * @see WP_Query::is_day()
    305247 * @since 1.5.0
    306248 * @uses $wp_query
     
    311253        global $wp_query;
    312254
    313         return $wp_query->is_day;
    314 }
    315 
    316 /**
    317  * Whether current page query is feed URL.
    318  *
     255        return $wp_query->is_day();
     256}
     257
     258/**
     259 * Is the query for a feed?
     260 *
     261 * @see WP_Query::is_feed()
    319262 * @since 1.5.0
    320263 * @uses $wp_query
     
    325268        global $wp_query;
    326269
    327         return $wp_query->is_feed;
    328 }
    329 
    330 /**
    331  * Whether current page query is comment feed URL.
    332  *
     270        return $wp_query->is_feed();
     271}
     272
     273/**
     274 * Is the query for a comments feed?
     275 *
     276 * @see WP_Query::is_comments_feed()
    333277 * @since 3.0.0
    334278 * @uses $wp_query
     
    339283        global $wp_query;
    340284
    341         return $wp_query->is_comment_feed;
    342 }
    343 
    344 /**
    345  * Whether current page query is the front of the site.
    346  *
     285        return $wp_query->is_comment_feed();
     286}
     287
     288/**
     289 * Is the query for the front page of the site?
     290 *
     291 * This is for what is displayed at your site's main URL.
     292 *
     293 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
     294 *
     295 * If you set a static page for the front page of your site, this function will return
     296 * true when viewing that page.
     297 *
     298 * Otherwise the same as @see is_home()
     299 *
     300 * @see WP_Query::is_front_page()
    347301 * @since 2.5.0
    348302 * @uses is_home()
     
    352306 */
    353307function is_front_page() {
    354         // most likely case
    355         if ( 'posts' == get_option('show_on_front') && is_home() )
    356                 return true;
    357         elseif ( 'page' == get_option('show_on_front') && get_option('page_on_front') && is_page(get_option('page_on_front')) )
    358                 return true;
    359         else
    360                 return false;
    361 }
    362 
    363 /**
    364  * Whether current page view is the blog homepage.
    365  *
    366  * This is the page which is showing the time based blog content of your site
    367  * so if you set a static page for the front page of your site then this will
    368  * only be true on the page which you set as the "Posts page" in Reading Settings.
    369  *
     308        global $wp_query;
     309
     310        return $wp_query->is_front_page();
     311}
     312
     313/**
     314 * Is the query for the blog homepage?
     315 *
     316 * This is the page which shows the time based blog content of your site.
     317 *
     318 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
     319 *
     320 * If you set a static page for the front page of your site, this function will return
     321 * true only on the page you set as the "Posts page".
     322 *
     323 * @see is_front_page()
     324 *
     325 * @see WP_Query::is_home()
    370326 * @since 1.5.0
    371327 * @uses $wp_query
     
    376332        global $wp_query;
    377333
    378         return $wp_query->is_home;
    379 }
    380 
    381 /**
    382  * Whether current page query contains a month.
    383  *
     334        return $wp_query->is_home();
     335}
     336
     337/**
     338 * Is the query for a month archive?
     339 *
     340 * @see WP_Query::is_month()
    384341 * @since 1.5.0
    385342 * @uses $wp_query
     
    390347        global $wp_query;
    391348
    392         return $wp_query->is_month;
    393 }
    394 
    395 /**
    396  * Whether query is page or contains given page(s).
    397  *
    398  * Calls the function without any parameters will only test whether the current
    399  * query is of the page type. Either a list or a single item can be tested
    400  * against for whether the query is a page and also is the value or one of the
    401  * values in the page parameter.
    402  *
    403  * The parameter can contain the page ID, page title, or page name. The
    404  * parameter can also be an array of those three values.
    405  *
     349        return $wp_query->is_month();
     350}
     351
     352/**
     353 * Is the query for a single Page?
     354 *
     355 * If the $page parameter is specified, this function will additionally
     356 * check if the query is for one of the Pages specified.
     357 *
     358 * @see is_single()
     359 * @see is_singular()
     360 *
     361 * @see WP_Query::is_single()
    406362 * @since 1.5.0
    407363 * @uses $wp_query
    408364 *
    409  * @param mixed $page Either page or list of pages to test against.
     365 * @param mixed $page Page ID, title, slug, or array of Page IDs, titles, and slugs.
    410366 * @return bool
    411367 */
    412 function is_page($page = '') {
    413         global $wp_query;
    414 
    415         if ( !$wp_query->is_page )
    416                 return false;
    417 
    418         if ( empty($page) )
    419                 return true;
    420 
    421         $page_obj = $wp_query->get_queried_object();
    422 
    423         $page = (array) $page;
    424 
    425         if ( in_array( $page_obj->ID, $page ) )
    426                 return true;
    427         elseif ( in_array( $page_obj->post_title, $page ) )
    428                 return true;
    429         else if ( in_array( $page_obj->post_name, $page ) )
    430                 return true;
    431 
    432         return false;
    433 }
    434 
    435 /**
    436  * Whether query contains multiple pages for the results.
    437  *
     368function is_page( $page = '' ) {
     369        global $wp_query;
     370
     371        return $wp_query->is_page( $page );
     372}
     373
     374/**
     375 * Is the query for paged result and not for the first page?
     376 *
     377 * @see WP_Query::is_paged()
    438378 * @since 1.5.0
    439379 * @uses $wp_query
     
    444384        global $wp_query;
    445385
    446         return $wp_query->is_paged;
    447 }
    448 
    449 /**
    450  * Whether the current page was created by a plugin.
    451  *
    452  * The plugin can set this by using the global $plugin_page and setting it to
    453  * true.
    454  *
     386        return $wp_query->is_paged();
     387}
     388
     389/**
     390 * Is the query for a post or page preview?
     391 *
     392 * @see WP_Query::is_preview()
     393 * @since 2.0.0
     394 * @uses $wp_query
     395 *
     396 * @return bool
     397 */
     398function is_preview() {
     399        global $wp_query;
     400
     401        return $wp_query->is_preview();
     402}
     403
     404/**
     405 * Is the query for the robots file?
     406 *
     407 * @see WP_Query::is_robots()
     408 * @since 2.1.0
     409 * @uses $wp_query
     410 *
     411 * @return bool
     412 */
     413function is_robots() {
     414        global $wp_query;
     415
     416        return $wp_query->is_robots();
     417}
     418
     419/**
     420 * Is the query for a search?
     421 *
     422 * @see WP_Query::is_search()
    455423 * @since 1.5.0
    456  * @global bool $plugin_page Used by plugins to tell the query that current is a plugin page.
     424 * @uses $wp_query
    457425 *
    458426 * @return bool
    459427 */
    460 function is_plugin_page() {
    461         global $plugin_page;
    462 
    463         if ( isset($plugin_page) )
    464                 return true;
    465 
    466         return false;
    467 }
    468 
    469 /**
    470  * Whether the current query is preview of post or page.
    471  *
    472  * @since 2.0.0
    473  * @uses $wp_query
    474  *
     428function is_search() {
     429        global $wp_query;
     430
     431        return $wp_query->is_search();
     432}
     433
     434/**
     435 * Is the query for a single post?
     436 *
     437 * If the $post parameter is specified, this function will additionally
     438 * check if the query is for one of the Posts specified.
     439 *
     440 * Can also be used for attachments or any other post type except pages.
     441 *
     442 * @see is_page()
     443 * @see is_singular()
     444 *
     445 * @see WP_Query::is_single()
     446 * @since 1.5.0
     447 * @uses $wp_query
     448 *
     449 * @param mixed $post Post ID, title, slug, or array of Post IDs, titles, and slugs.
    475450 * @return bool
    476451 */
    477 function is_preview() {
    478         global $wp_query;
    479 
    480         return $wp_query->is_preview;
    481 }
    482 
    483 /**
    484  * Whether the current query post is robots.
    485  *
    486  * @since 2.1.0
    487  * @uses $wp_query
    488  *
     452function is_single( $post = '' ) {
     453        global $wp_query;
     454
     455        return $wp_query->is_single( $post );
     456}
     457
     458/**
     459 * Is the query for a single post of any post type (post, attachment, page, ... )?
     460 *
     461 * If the $post_types parameter is specified, this function will additionally
     462 * check if the query is for one of the Posts Types specified.
     463 *
     464 * @see is_page()
     465 * @see is_single()
     466 *
     467 * @see WP_Query::is_singular()
     468 * @since 1.5.0
     469 * @uses $wp_query
     470 *
     471 * @param mixed $post_types Optional. Post Type or array of Post Types
    489472 * @return bool
    490473 */
    491 function is_robots() {
    492         global $wp_query;
    493 
    494         return $wp_query->is_robots;
    495 }
    496 
    497 /**
    498  * Whether current query is the result of a user search.
    499  *
     474function is_singular( $post_types = '' ) {
     475        global $wp_query;
     476
     477        return $wp_query->is_singular( $post_types );
     478}
     479
     480/**
     481 * Is the query for a specific time?
     482 *
     483 * @see WP_Query::is_time()
    500484 * @since 1.5.0
    501485 * @uses $wp_query
     
    503487 * @return bool
    504488 */
    505 function is_search() {
    506         global $wp_query;
    507 
    508         return $wp_query->is_search;
    509 }
    510 
    511 /**
    512  * Whether the current page query is single page.
    513  *
    514  * The parameter can contain the post ID, post title, or post name. The
    515  * parameter can also be an array of those three values.
    516  *
    517  * This applies to other post types, attachments, pages, posts. Just means that
    518  * the current query has only a single object.
    519  *
     489function is_time() {
     490        global $wp_query;
     491
     492        return $wp_query->is_time();
     493}
     494
     495/**
     496 * Is the query for a trackback endpoint call?
     497 *
     498 * @see WP_Query::is_trackback()
    520499 * @since 1.5.0
    521500 * @uses $wp_query
    522501 *
    523  * @param mixed $post Either post or list of posts to test against.
    524502 * @return bool
    525503 */
    526 function is_single($post = '') {
    527         global $wp_query;
    528 
    529         if ( !$wp_query->is_single )
    530                 return false;
    531 
    532         if ( empty($post) )
    533                 return true;
    534 
    535         $post_obj = $wp_query->get_queried_object();
    536 
    537         $post = (array) $post;
    538 
    539         if ( in_array( $post_obj->ID, $post ) )
    540                 return true;
    541         elseif ( in_array( $post_obj->post_title, $post ) )
    542                 return true;
    543         elseif ( in_array( $post_obj->post_name, $post ) )
    544                 return true;
    545 
    546         return false;
    547 }
    548 
    549 /**
    550  * Whether is single post, is a page, or is an attachment.
    551  *
     504function is_trackback() {
     505        global $wp_query;
     506
     507        return $wp_query->is_trackback();
     508}
     509
     510/**
     511 * Is the query for a specific year?
     512 *
     513 * @see WP_Query::is_year()
    552514 * @since 1.5.0
    553515 * @uses $wp_query
    554516 *
    555  * @param string|array $post_types Optional. Post type or types to check in current query.
    556517 * @return bool
    557518 */
    558 function is_singular($post_types = '') {
    559         global $wp_query;
    560 
    561         if ( empty($post_types) || !$wp_query->is_singular )
    562                 return $wp_query->is_singular;
    563 
    564         $post_obj = $wp_query->get_queried_object();
    565 
    566         return in_array($post_obj->post_type, (array) $post_types);
    567 }
    568 
    569 /**
    570  * Whether the query contains a time.
    571  *
     519function is_year() {
     520        global $wp_query;
     521
     522        return $wp_query->is_year();
     523}
     524
     525/**
     526 * Is the query a 404 (returns no results)?
     527 *
     528 * @see WP_Query::is_404()
    572529 * @since 1.5.0
    573530 * @uses $wp_query
     
    575532 * @return bool
    576533 */
    577 function is_time() {
    578         global $wp_query;
    579 
    580         return $wp_query->is_time;
    581 }
    582 
    583 /**
    584  * Whether the query is a trackback.
    585  *
    586  * @since 1.5.0
    587  * @uses $wp_query
    588  *
    589  * @return bool
    590  */
    591 function is_trackback() {
    592         global $wp_query;
    593 
    594         return $wp_query->is_trackback;
    595 }
    596 
    597 /**
    598  * Whether the query contains a year.
    599  *
    600  * @since 1.5.0
    601  * @uses $wp_query
    602  *
    603  * @return bool
    604  */
    605 function is_year() {
    606         global $wp_query;
    607 
    608         return $wp_query->is_year;
    609 }
    610 
    611 /**
    612  * Whether current page query is a 404 and no results for WordPress query.
    613  *
    614  * @since 1.5.0
    615  * @uses $wp_query
    616  *
    617  * @return bool True, if nothing is found matching WordPress Query.
    618  */
    619534function is_404() {
    620535        global $wp_query;
    621536
    622         return $wp_query->is_404;
     537        return $wp_query->is_404();
    623538}
    624539
     
    27882703                }
    27892704        }
     2705
     2706        /**
     2707         * Is the query for an archive page?
     2708         *
     2709         * Month, Year, Category, Author, ...
     2710         *
     2711         * @since 3.1.0
     2712         *
     2713         * @return bool
     2714         */
     2715        function is_archive() {
     2716                return (bool) $this->is_archive;
     2717        }
     2718
     2719        /**
     2720         * Is the query for an attachment page?
     2721         *
     2722         * @since 3.1.0
     2723         *
     2724         * @return bool
     2725         */
     2726        function is_attachment() {
     2727                return (bool) $this->is_attachment;
     2728        }
     2729
     2730        /**
     2731         * Is the query for an author archive page?
     2732         *
     2733         * If the $author parameter is specified, this function will additionally
     2734         * check if the query is for one of the authors specified.
     2735         *
     2736         * @since 3.1.0
     2737         *
     2738         * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
     2739         * @return bool
     2740         */
     2741        function is_author( $author = '' ) {
     2742                if ( !$this->is_author )
     2743                        return false;
     2744
     2745                if ( empty($author) )
     2746                        return true;
     2747
     2748                $author_obj = $this->get_queried_object();
     2749
     2750                $author = (array) $author;
     2751
     2752                if ( in_array( $author_obj->ID, $author ) )
     2753                        return true;
     2754                elseif ( in_array( $author_obj->nickname, $author ) )
     2755                        return true;
     2756                elseif ( in_array( $author_obj->user_nicename, $author ) )
     2757                        return true;
     2758
     2759                return false;
     2760        }
     2761
     2762        /**
     2763         * Is the query for a category archive page?
     2764         *
     2765         * If the $category parameter is specified, this function will additionally
     2766         * check if the query is for one of the categories specified.
     2767         *
     2768         * @since 3.1.0
     2769         *
     2770         * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
     2771         * @return bool
     2772         */
     2773        function is_category( $category = '' ) {
     2774                if ( !$this->is_category )
     2775                        return false;
     2776
     2777                if ( empty($category) )
     2778                        return true;
     2779
     2780                $cat_obj = $this->get_queried_object();
     2781
     2782                $category = (array) $category;
     2783
     2784                if ( in_array( $cat_obj->term_id, $category ) )
     2785                        return true;
     2786                elseif ( in_array( $cat_obj->name, $category ) )
     2787                        return true;
     2788                elseif ( in_array( $cat_obj->slug, $category ) )
     2789                        return true;
     2790
     2791                return false;
     2792        }
     2793
     2794        /**
     2795         * Is the query for a tag archive page?
     2796         *
     2797         * If the $tag parameter is specified, this function will additionally
     2798         * check if the query is for one of the tags specified.
     2799         *
     2800         * @since 3.1.0
     2801         *
     2802         * @param mixed $slug Optional. Tag slug or array of slugs.
     2803         * @return bool
     2804         */
     2805        function is_tag( $slug = '' ) {
     2806                if ( !$this->is_tag )
     2807                        return false;
     2808
     2809                if ( empty( $slug ) )
     2810                        return true;
     2811
     2812                $tag_obj = $this->get_queried_object();
     2813
     2814                $slug = (array) $slug;
     2815
     2816                if ( in_array( $tag_obj->slug, $slug ) )
     2817                        return true;
     2818
     2819                return false;
     2820        }
     2821
     2822        /**
     2823         * Is the query for a taxonomy archive page?
     2824         *
     2825         * If the $taxonomy parameter is specified, this function will additionally
     2826         * check if the query is for that specific $taxonomy.
     2827         *
     2828         * If the $term parameter is specified in addition to the $taxonomy parameter,
     2829         * this function will additionally check if the query is for one of the terms
     2830         * specified.
     2831         *
     2832         * @since 3.1.0
     2833         *
     2834         * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
     2835         * @param mixed $term. Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
     2836         * @return bool
     2837         */
     2838        function is_tax( $taxonomy = '', $term = '' ) {
     2839                global $wp_taxonomies;
     2840
     2841                if ( !$this->is_tax )
     2842                        return false;
     2843
     2844                if ( empty( $taxonomy ) )
     2845                        return true;
     2846
     2847                $queried_object = $this->get_queried_object();
     2848                $tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy );
     2849                $term_array = (array) $term;
     2850
     2851                if ( empty( $term ) ) // Only a Taxonomy provided
     2852                        return isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array );
     2853
     2854                return isset( $queried_object->term_id ) &&
     2855                        count( array_intersect(
     2856                                array( $queried_object->term_id, $queried_object->name, $queried_object->slug ),
     2857                                $term_array
     2858                        ) );
     2859        }
     2860
     2861        /**
     2862         * Whether the current URL is within the comments popup window.
     2863         *
     2864         * @since 3.1.0
     2865         *
     2866         * @return bool
     2867         */
     2868        function is_comments_popup() {
     2869                return (bool) $this->is_comments_popup;
     2870        }
     2871
     2872        /**
     2873         * Is the query for a date archive?
     2874         *
     2875         * @since 3.1.0
     2876         *
     2877         * @return bool
     2878         */
     2879        function is_date() {
     2880                return (bool) $this->is_date;
     2881        }
     2882
     2883
     2884        /**
     2885         * Is the query for a day archive?
     2886         *
     2887         * @since 3.1.0
     2888         *
     2889         * @return bool
     2890         */
     2891        function is_day() {
     2892                return (bool) $this->is_day;
     2893        }
     2894
     2895        /**
     2896         * Is the query for a feed?
     2897         *
     2898         * @since 3.1.0
     2899         *
     2900         * @return bool
     2901         */
     2902        function is_feed() {
     2903                return (bool) $this->is_feed;
     2904        }
     2905
     2906        /**
     2907         * Is the query for a comments feed?
     2908         *
     2909         * @since 3.1.0
     2910         *
     2911         * @return bool
     2912         */
     2913        function is_comment_feed() {
     2914                return (bool) $this->is_comment_feed;
     2915        }
     2916
     2917        /**
     2918         * Is the query for the front page of the site?
     2919         *
     2920         * This is for what is displayed at your site's main URL.
     2921         *
     2922         * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
     2923         *
     2924         * If you set a static page for the front page of your site, this function will return
     2925         * true when viewing that page.
     2926         *
     2927         * Otherwise the same as @see WP_Query::is_home()
     2928         *
     2929         * @since 3.1.0
     2930         * @uses is_home()
     2931         * @uses get_option()
     2932         *
     2933         * @return bool True, if front of site.
     2934         */
     2935        function is_front_page() {
     2936                // most likely case
     2937                if ( 'posts' == get_option( 'show_on_front') && $this->is_home() )
     2938                        return true;
     2939                elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) )
     2940                        return true;
     2941                else
     2942                        return false;
     2943        }
     2944
     2945        /**
     2946         * Is the query for the blog homepage?
     2947         *
     2948         * This is the page which shows the time based blog content of your site.
     2949         *
     2950         * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
     2951         *
     2952         * If you set a static page for the front page of your site, this function will return
     2953         * true only on the page you set as the "Posts page".
     2954         *
     2955         * @see WP_Query::is_front_page()
     2956         *
     2957         * @since 3.1.0
     2958         *
     2959         * @return bool True if blog view homepage.
     2960         */
     2961        function is_home() {
     2962                return (bool) $this->is_home;
     2963        }
     2964
     2965        /**
     2966         * Is the query for a month archive?
     2967         *
     2968         * @since 3.1.0
     2969         *
     2970         * @return bool
     2971         */
     2972        function is_month() {
     2973                return (bool) $this->is_month;
     2974        }
     2975
     2976        /**
     2977         * Is the query for a single Page?
     2978         *
     2979         * If the $page parameter is specified, this function will additionally
     2980         * check if the query is for one of the Pages specified.
     2981         *
     2982         * @see WP_Query::is_single()
     2983         * @see WP_Query::is_singular()
     2984         *
     2985         * @since 3.1.0
     2986         *
     2987         * @param mixed $page Page ID, title, slug, or array of Page IDs, titles, and slugs.
     2988         * @return bool
     2989         */
     2990        function is_page( $page = '' ) {
     2991                if ( !$this->is_page )
     2992                        return false;
     2993
     2994                if ( empty( $page ) )
     2995                        return true;
     2996
     2997                $page_obj = $this->get_queried_object();
     2998
     2999                $page = (array) $page;
     3000
     3001                if ( in_array( $page_obj->ID, $page ) )
     3002                        return true;
     3003                elseif ( in_array( $page_obj->post_title, $page ) )
     3004                        return true;
     3005                else if ( in_array( $page_obj->post_name, $page ) )
     3006                        return true;
     3007
     3008                return false;
     3009        }
     3010
     3011        /**
     3012         * Is the query for paged result and not for the first page?
     3013         *
     3014         * @since 3.1.0
     3015         *
     3016         * @return bool                                                                 
     3017         */
     3018        function is_paged() {
     3019                return (bool) $this->is_paged;
     3020        }
     3021
     3022        /**
     3023         * Is the query for a post or page preview?
     3024         *
     3025         * @since 3.1.0
     3026         *
     3027         * @return bool
     3028         */
     3029        function is_preview() {
     3030                return (bool) $this->is_preview;
     3031        }
     3032
     3033        /**
     3034         * Is the query for the robots file?
     3035         *
     3036         * @since 3.1.0
     3037         *
     3038         * @return bool
     3039         */
     3040        function is_robots() {
     3041                return (bool) $this->is_robots;
     3042        }
     3043
     3044        /**
     3045         * Is the query for a search?
     3046         *
     3047         * @since 3.1.0
     3048         *
     3049         * @return bool
     3050         */
     3051        function is_search() {
     3052                return (bool) $this->is_search;
     3053        }
     3054
     3055        /**
     3056         * Is the query for a single post?
     3057         *
     3058         * If the $post parameter is specified, this function will additionally
     3059         * check if the query is for one of the Posts specified.
     3060         *
     3061         * Can also be used for attachments or any other post type except pages.
     3062         *
     3063         * @see WP_Query::is_page()
     3064         * @see WP_Query::is_singular()
     3065         *
     3066         * @since 3.1.0
     3067         *
     3068         * @param mixed $post Post ID, title, slug, or array of Post IDs, titles, and slugs.
     3069         * @return bool
     3070         */
     3071        function is_single( $post = '' ) {
     3072                if ( !$this->is_single )
     3073                        return false;
     3074
     3075                if ( empty($post) )
     3076                        return true;
     3077
     3078                $post_obj = $this->get_queried_object();
     3079
     3080                $post = (array) $post;
     3081
     3082                if ( in_array( $post_obj->ID, $post ) )
     3083                        return true;
     3084                elseif ( in_array( $post_obj->post_title, $post ) )
     3085                        return true;
     3086                elseif ( in_array( $post_obj->post_name, $post ) )
     3087                        return true;
     3088
     3089                return false;
     3090        }
     3091
     3092        /**
     3093         * Is the query for a single post of any post type (post, attachment, page, ... )?
     3094         *
     3095         * If the $post_types parameter is specified, this function will additionally
     3096         * check if the query is for one of the Posts Types specified.
     3097         *
     3098         * @see WP_Query::is_page()
     3099         * @see WP_Query::is_single()
     3100         *
     3101         * @since 3.1.0
     3102         *
     3103         * @param mixed $post_types Optional. Post Type or array of Post Types
     3104         * @return bool
     3105         */
     3106        function is_singular( $post_types = '' ) {
     3107                if ( empty( $post_types ) || !$this->is_singular )
     3108                        return $this->is_singular;
     3109
     3110                $post_obj = $this->get_queried_object();
     3111
     3112                return in_array( $post_obj->post_type, (array) $post_types );
     3113        }
     3114
     3115        /**
     3116         * Is the query for a specific time?
     3117         *
     3118         * @since 3.1.0
     3119         *
     3120         * @return bool
     3121         */
     3122        function is_time() {
     3123                return (bool) $this->is_time;
     3124        }
     3125
     3126        /**
     3127         * Is the query for a trackback endpoint call?
     3128         *
     3129         * @since 3.1.0
     3130         *
     3131         * @return bool
     3132         */
     3133        function is_trackback() {
     3134                return (bool) $this->is_trackback;
     3135        }
     3136
     3137        /**
     3138         * Is the query for a specific year?
     3139         *
     3140         * @since 3.1.0
     3141         *
     3142         * @return bool
     3143         */
     3144        function is_year() {
     3145                return (bool) $this->is_year;
     3146        }
     3147
     3148        /**
     3149         * Is the query a 404 (returns no results)?
     3150         *
     3151         * @since 3.1.0
     3152         *
     3153         * @return bool
     3154         */
     3155        function is_404() {
     3156                return (bool) $this->is_404;
     3157        }
    27903158}
    27913159
Note: See TracChangeset for help on using the changeset viewer.