How can a subpage inherit the style of the parent on WordPress?

I am designing a theme where every page has a different text, background and other elements’ color. I was able to style every single page (and post category related) with these:

 <?php if ( is_home() || is_search() || is_archive ()) 
    {
    ?>
    <link rel="stylesheet" href="<?php bloginfo('template_url')?>/css/home.css" type="text/css" media="screen" />
    <?php } elseif( is_category( 'Turismo a Bra' ) || is_page('Turismo a Bra')) 
    {
    ?>
    <link rel="stylesheet" href="<?php bloginfo('template_url')?>/css/turismo-a-bra.css" type="text/css" media="screen" />      
    <?php } elseif ( is_category ('Eventi') || is_page('Eventi'))
    {
    ?>
    <link rel="stylesheet" href="<?php bloginfo('template_url')?>/css/eventi.css" type="text/css" media="screen" />
    <?php } elseif ( is_category ('Arte e Cultura') || is_page('Arte e Cultura'))
    {
    ?>
    <link rel="stylesheet" href="<?php bloginfo('template_url')?>/css/arte-e-cultura.css" type="text/css" media="screen" />
    <?php } elseif ( is_category ('Enogastronomia')|| is_page('Enogastronomia'))
    {
    ?>
    <link rel="stylesheet" href="<?php bloginfo('template_url')?>/css/enogastronomia.css" type="text/css" media="screen" />
<?php } elseif ( is_category ('Natura')|| is_page('Natura'))
    {
    ?>
    <link rel="stylesheet" href="<?php bloginfo('template_url')?>/css/natura.css" type="text/css" media="screen" />
    <?php } else {  ?>

    <?php } ?>

The problem comes when I have (and i have a lot) of subpages. I want them to be styled as their parents. I though that WP had the is_sub_page(#), but no luck.

Read More

Do you know what I should add in the condition to make header understand when it is dealing with a subpage and, in that case, get the ID of the parent and based on that style the page.

I am a novice with php and wordpress, it makes sense in my head, but i dont know how to phrase it.

Thanks a lot, an example is here(the subpages are in the upper right side.

Related posts

Leave a Reply

1 comment

  1. To check whether the post decends from a page with a certain category or page title then you could get its parent and check, eg:

    in_category( 'Turismo a Bra', $post->post_parent )
    

    As you already have a lot of code and you are doing this multiple times it may be best to encapsulate your entire check within a function:

    function needs_style( $style, $the_post ){
        $needs_style = false;
        //check details of this post first
        if( $the_post->post_title == $style ){        //does the same as in_page()
            $needs_style = true;
        } 
        elseif( in_category( $style, $the_post ) ){
            $needs_style = true;
        }
        //otherwise check parent if post has one - this is done recursively 
        elseif( $the_post->post_parent ){
            $the_parent = get_post( $the_post->post_parent );
            $needs_style = needs_style( $style, $the_parent );
        }
        return $needs_style; 
    }
    

    So your code would look something like:

    if ( is_home() || is_search() || is_archive ()) { 
        //set stylesheet
    }
    elseif( needs_style( 'Turismo a Bra', $post ) ) {
        //set stylesheet
    }
    elseif( needs_style( 'Eventi', $post ) ) {
        //set stylesheet
    }