Custom CSS in WordPress parent and child pages

Trust me, I've been looking for an answer to this for several days in a row. I believe the answer is simple, but I cannot get it to work. I will try to be as specific as possible.

I learned how to set CSS for specific WordPress pages, however I need to set CSS for parent WordPress pages and their children. While this can be achieved by including each page in my code, I know there must be a more efficient way since I have about 20 pages under each parent page.

An example of what I'm looking for can be found here: (original site) www.viewmonthealth.com

Where I am trying to do this: www.notthemama.net/prime/

Each main link leads to a different section with separate css.

Here's what I have:

<?php if (is_page('imaging')) { ?>
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/viewmont-imaging.css" type="text/css">
<?php } elseif (is_page('labs')) { ?>
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/viewmont-labs.css" type="text/css">
<?php } else { ?>
    <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" />
<?php } ?>

      

This works great for pages. I just need to include parent pages (what I have) and child pages (imaging / forms.php). How can i do this? Any help is appreciated!: D

+3


source to share


2 answers


you can try something with parent id

function is_child($pageID) { 
    global $post; 
    if( is_page() && ($post->post_parent==$pageID) ) {
               return true;
    } else { 
               return false; 
    }
}

      

putting this in your function and



<?php if (is_child(12)) { ?>
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/viewmont-imaging.css" type="text/css">
<?php } elseif (is_child(14)) { ?>
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/viewmont-labs.css" type="text/css">
<?php } else { ?>
    <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" />
<?php } ?>

      

Hope this is what you are looking for. :)

0


source


A much more compact approach uses the following conditional check, no need for a special function:



if ( $post->post_parent == 2 ) { ?>
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/viewmont-imaging.css" type="text/css">
<?php } ?>

      

0


source







All Articles