I created a WordPress template and now I would like to use that template for several pages, however I would like to have different background color on some of the elements such as footer, header etc... which will depend on the parent.

So lets say I have PARENT A (green background) and PARENT B (white background) now when I will create child of PARENT A I would like it to have green background automatically. I know something similar could be done with posts using

<div class="post <?php
foreach((get_the_category()) as $cat) {
echo $cat->cat_name . ' ';
} ?>">

I need some advice what would be the best way to do that.


3 Answers 3


If your theme is using the body_class(); function then you can target pages via CSS.

If you have parent page with the ID of 2 and the background you wanted to change was the .content div, you would simply use :

.parent-pageid-2 .content { background: green; }

Or something similar to that effect. This would work for as many child pages you have.


You can do something along the lines of the code below, having a different div depending on the page id. Put the style in your CSS obviously, but this paints the picture. Put the code in whatever php file you're applying this to.

<?php if (is_page('30')) { ?>
    <div id="page30" style="background:blue;">
<?php } else { ?>
    <div id="default" style="background:white;">
<?php } ?>
<!-- content inside div -->
  • but will only work for single pages and every time i will create new page i will need to go to source code and change it also, but thanks for suggestion
    – jmysona
    Apr 29, 2011 at 9:09

To add to what TomHarrigan wrote you can try something like post_parent, ... I'll assume you mean page parents.

$children = get_children( "post_type=page&post_parent=PARENT_A" );

if $children { 
<div id="page30" style="background:blue;">

You should have a look into using the Options API for this type of stuff. http://codex.wordpress.org/Options_API

Your Answer

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