Ticket #5430 (closed enhancement: fixed)

Opened 4 years ago

Last modified 4 years ago

Change is_page() to accept array as argument

Reported by: azaozz Owned by: anonymous
Priority: normal Milestone: 2.5
Component: Template Version: 2.3.1
Severity: normal Keywords: has-patch
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

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

Change History

azaozz4 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;

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

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

Looks good to me.

comment:4   matt4 years ago

+1.

comment:5   ryan4 years ago

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

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

Note: See TracTickets for help on using tickets.