WordPress.org

Make WordPress Core

Opened 11 years ago

Closed 11 years ago

Last modified 14 months ago

#5430 closed enhancement (fixed)

Change is_page() to accept array as argument

Reported by: azaozz Owned by:
Milestone: 2.5 Priority: normal
Severity: normal Version: 2.3.1
Component: Template Keywords: has-patch
Focuses: Cc:

Description

I often see

if ( is_page(3) || is_page(8) || is_page(15) )

... etc. in templates. Same goes for is_single(), is_author(), is_category() and perhaps is_tag(). It would be much cleaner/easier if these conditional tags can accept an array as argument as well. The attached patch changes the is_page() function to accept an array of page IDs, page slugs or page titles and can be used like this:

$my_pages = array ( 3, 'another-page', 'Special Page' );

if ( is_page( $my_pages ) )
    $do_something...

It is (of course) backwards compatible and there's no noticeable performance hit at all, as is_page() is called just once instead of several times.

Attachments (1)

query.diff (366 bytes) - added by azaozz 11 years ago.

Download all attachments as: .zip

Change History (7)

@azaozz
11 years ago

#1 @azaozz
11 years ago

The attachment didn't come up as expected, sry. Here's the change
line 155 in query.php, instead of:

if ( $page == $page_obj->ID )
    return true;
elseif ( $page == $page_obj->post_title )
    return true;
else if ( $page == $page_obj->post_name )
    return true;

add:

$page = (array) $page;
    
if ( in_array( $page_obj->ID, $page ) )
    return true;
elseif ( in_array( $page_obj->post_title, $page ) )
    return true;
elseif ( in_array( $page_obj->post_name, $page ) )
    return true;

#2 @johnbillion
11 years ago

+1, nice work azaozz. I can see this coming in very handy.

#3 @Viper007Bond
11 years ago

  • Keywords has-patch added
  • Milestone changed from 2.5 to 2.4
  • Version set to 2.3.1

Looks good to me.

#4 @matt
11 years ago

+1.

#5 @ryan
11 years ago

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

(In [6397]) Accept array of pages for is_page(). Props azaozz. fixes #5430

This ticket was mentioned in Slack in #core-editor by alexander-botteram. View the logs.


14 months ago

Note: See TracTickets for help on using tickets.