Make WordPress Core


Ignore:
Timestamp:
01/22/2014 10:30:36 PM (11 years ago)
Author:
wonderboymusic
Message:

Let is_attachment() accept an $attachment parameter, similar to is_page() and is_single(). Adds Unit Tests for all 3.

Props alex-ye for the initial patch.
Fixes #24257.

File:
1 edited

Legend:

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

    r26932 r27016  
    176176 * @uses $wp_query
    177177 *
     178 * @param mixed $attachment Attachment ID, title, slug, or array of such.
    178179 * @return bool
    179180 */
    180 function is_attachment() {
     181function is_attachment( $attachment = '' ) {
    181182    global $wp_query;
    182183
     
    186187    }
    187188
    188     return $wp_query->is_attachment();
     189    return $wp_query->is_attachment( $attachment );
    189190}
    190191
     
    33883389     * @since 3.1.0
    33893390     *
     3391         * @param mixed $attachment Attachment ID, title, slug, or array of such.
    33903392     * @return bool
    33913393     */
    3392     function is_attachment() {
    3393         return (bool) $this->is_attachment;
     3394    function is_attachment( $attachment = '' ) {
     3395        if ( ! $this->is_attachment ) {
     3396            return false;
     3397        }
     3398
     3399        if ( empty( $attachment ) ) {
     3400            return true;
     3401        }
     3402
     3403        $attachment = (array) $attachment;
     3404
     3405        $post_obj = $this->get_queried_object();
     3406
     3407        if ( in_array( $post_obj->ID, $attachment ) ) {
     3408            return true;
     3409        } elseif ( in_array( $post_obj->post_title, $attachment ) ) {
     3410            return true;
     3411        } elseif ( in_array( $post_obj->post_name, $attachment ) ) {
     3412            return true;
     3413        }
     3414        return false;
    33943415    }
    33953416
Note: See TracChangeset for help on using the changeset viewer.