1

Here's my challenge...

I have 52 pages that require a full width background image that will be rendered via a .css style..

So, what I will know are all the Page ID Numbers. The below code I believe is a tidy way of doing what I am trying to achieve.

My question is, rather than have 52 instances of the below code, is it possible to simply place all Page ID's and $classes of 'css' separated by commas?

add_filter('body_class','wpsites_specific_page_body_class');
/**
* @author Brad Dalton - WP Sites
*
* @link http://wpsites.net/web-design/style-images-custom-body-class/
*/
function wpsites_specific_page_body_class($classes) {
 if(is_page('007') ) {
    $classes[] = 'demo-class';
return $classes;
   }
}

Thank you for all direction.

2

I would consider making it more flexible by marking the pages in question with post meta or custom taxonomy. That way you don't need to change a PHP code to choose what pages should have some custom look.

Here's an example, if we mark the pages with the wpse-layout custom field with the value 1 (assuming there could be more layout options):

add_filter( 'body_class', function( $classes )
{
    // Only target pages
    if( ! is_page() )
        return $classes;

    // Get the 'wpse_layout' post meta value for the current page
    $layout = get_post_meta( get_queried_object_id(), 'wpse_layout', true );

    if( empty( $layout ) )
        return $classes;

    // Inject the 'wpse-layout-1' body class, 
    // if the custom field 'wpse-layout' has the value 1
    $classes[] = sprintf( 'wpse-layout-%d', $layout );

    return $classes;

} );

This could be further adjusted in various ways, e.g. with some custom UI.

  • I need to learn about post meta + taxonomy. – henry Aug 8 '17 at 14:09
  • It think that kind of approach would be much more flexible than something like is_page( [1, 2, ..., 50 ] ) – birgire Aug 8 '17 at 14:11
  • Updated with an example @henry – birgire Aug 8 '17 at 14:34
0

Try this.

function wpsites_specific_page_body_class($classes) {
    global $post;
    $posts = [7=>'demo-class',8=>'another-class']
     if (array_key_exists($post->ID,$posts))
        $classes[] = $posts[$post->ID];
        return $classes;
     }
 }

You can add any number of posts to the $posts array. It will set the class assigned to the post id.

  • is there a typo with the number before 'another-class', should that be 8 according to your example (thanks for replying BTW). – henry Aug 8 '17 at 14:08
0

first get all page id click here for more inforamtion

add_filter('body_class','wpsites_specific_page_body_class');
/**
* @author Brad Dalton - WP Sites
*
* @link http://wpsites.net/web-design/style-images-custom-body-class/
*/
function wpsites_specific_page_body_class($classes) {
  $page_ids= get_all_page_ids();
 if(is_page($page_ids) ) {
    $classes[] = 'demo-class';
return $classes;
   }
}
0

I would certainly go with Birgir's answer, it's the right approach. But since you asked, what you want to do can be done by using an array.

Store your page IDs in an array, and then do a conditional:

$ids = array( '1', '23', '53', '99' ... '1001' );
if( is_page( $ids ) ) {
    // Your code here
}

The is_page() function accepts either an integer, a string or an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.