Ticket #5430 (closed enhancement: fixed)

Opened 7 months ago

Last modified 7 months ago

Change is_page() to accept array as argument

Reported by: azaozz Assigned to: 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 (366 bytes) - added by azaozz on 12/06/07 23:34:22.

Change History

12/06/07 23:34:22 changed by azaozz

  • attachment query.diff added.

12/06/07 23:53:47 changed by azaozz

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;

12/07/07 00:59:32 changed by johnbillion

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

12/07/07 01:33:49 changed by Viper007Bond

  • keywords set to has-patch.
  • version set to 2.3.1.
  • milestone changed from 2.5 to 2.4.

Looks good to me.

12/16/07 22:10:57 changed by matt

+1.

12/17/07 06:21:44 changed by ryan

  • status changed from new to closed.
  • resolution set to fixed.

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