WordPress.org

Ready to get started?Download WordPress

Forums

Subpage get parent page slug (4 posts)

  1. Darfuria
    Member
    Posted 5 years ago #

    I'm building a template and have the following code

    <?php if (is_page()) { ?>
    <?php $cssdir = $post->post_name; ?>
    <link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/page.css" type="text/css" media="screen" />
    <link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/<?php echo $cssdir; ?>/style.css" type="text/css" media="screen" />
    <?php } ?>

    to select stylesheets based on the pagenames, which works fine and dandy, until you load a subpage.

    On a page the code generates the css URL like this:

    http://www.mywebsite.com/wp-content/themes/mytheme/pagename/style.css - fine, but on a subpage:

    http://www.mywebsite.com/wp-content/themes/mytheme/pagename/subpagename/style.css

    I just want it to grab the parent page's style.css, which could be done by the parent page's slug, but I'm not sure how to do this.

  2. Darfuria
    Member
    Posted 5 years ago #

    Resolved thanks to KMxRetro of RewiredMind

    <?php if (is_page()) { ?>
    <?php $cssdir = get_permalink(); ?>
    <?php $holder = explode("/",$cssdir) ?>
    <link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/page.css" type="text/css" media="screen" />
    <link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/<?php echo $holder[3]; ?>/style.css" type="text/css" media="screen" />
    <?php } ?>

    May work differently on a different URL structure, but works on my template - so far so good.

  3. barriecalver
    Member
    Posted 5 years ago #

    This is an alternative I found and constructed from all the help on the forums here so thought this was the best place to post as it was so very close but no cigar for me:

    <?php
    $parent = get_page($post->post_parent);
    $grandparent = $parent->post_parent;
    ?>
    
    <?php
    
    if ( is_page('corporate-training') || $post->post_parent == '59' || $grandparent == '59' ) {
        $stylesetting = 'training.css';
    
    } elseif ( is_page('hotbedfest') || $post->post_parent == '55' || $grandparent == '55' ) {
        $stylesetting = 'hotbed.css';
    
    } elseif ( is_page('productions') || $post->post_parent == '10' || $grandparent == '10' ) {
        $stylesetting = 'productions.css';
    
    } else {
        $stylesetting = 'style.css'; // Fall-through
    }	
    
    ?>
    
    <link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/<?php echo $stylesetting; ?>" type="text/css" media="screen" />

    Thanks to all you great people..

  4. refugee43
    Member
    Posted 5 years ago #

    I am having a similar problem. I used a conditional to change the banner, background, etc. on a couple of pages. However, the subpages to those pages revert back to the default style. In my header.php I posted the following code:

    <?php if (is_page('creation')) { ?>
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/style_creation.css" type="text/css">
    <?php }; ?>
    <?php if (is_page('anarchism')) { ?>
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/style_anarchism.css" type="text/css">
    <?php }; ?>

    This works fine for those particular pages. But like I said, the subpages revert to default. I am not quite sure how to get the subpages to follow suit.

Topic Closed

This topic has been closed to new replies.

About this Topic