15

I am trying to output a logo depending on which page is being viewed.

<?php if ( is_page('home') || is_page('services') ) { ?>
    <div class="col-md-2 col-sm-4">
        <?php ci_e_logo('<h1 class="logo ' . get_logo_class() . '">', '</h1>'); ?>
    </div>
<?php } 
else { ?>
    <div class="col-md-2 col-sm-4">
        <h1 class="logo imglogo">
            <a href="http://websiteaddress.com">
                <img src="<?php echo get_bloginfo('template_directory');?>/images/picturehere.png" alt="title here"></a>
            </h1>
    </div>
<?php } ?>

The code above works fine, but how do apply the logo image swap on the sub-pages of the 'services'?

7 Answers 7

15

You can do that with $post->post_parent. You will have to check if child page's parent is Services page. So this is how you will check it.

I assumed 123 in following code is page ID of your services page. Replace it with actual ID.

if ( 123 == $post->post_parent ) { ?>
    <div class="col-md-2 col-sm-4">
        <?php ci_e_logo('<h1 class="logo ' . get_logo_class() . '">', '</h1>'); ?>
    </div>
<?php }
1
  • 1
    Just a note that this won't work for descendants (ie, two sub-pages down), though it's still a better answer than the one selected IMO.
    – Nathan
    Commented May 25, 2017 at 19:21
8

You can get the Post page / post name using this method.

$parent = array_reverse(get_post_ancestors($post->ID));
$page_parent = get_post($parent[0]);
echo $page_parent->post_name;

you can use the condition as per your requirements.

1
  • Please format your code properly before posting.
    – Robert hue
    Commented Nov 24, 2014 at 5:18
6
<?php 
global $post;

if ( is_page('home') || is_page('services') ) { ?>
    <div class="col-md-2 col-sm-4">
        <?php ci_e_logo('<h1 class="logo ' . get_logo_class() . '">', '</h1>'); ?>
    </div>
<?php } 

elseif ( preg_match( '#^service(/.+)?$#', $wp->request ) ) { ?>
    <div class="col-md-2 col-sm-4">
        <?php ci_e_logo('<h1 class="logo ' . get_logo_class() . '">', '</h1>'); ?>
    </div>
<?php
}

else { ?>
    <div class="col-md-2 col-sm-4">
        <h1 class="logo imglogo">
            <a href="<?php echo esc_url( home_url( '/' ) ); ?>">
                <img src="<?php echo get_bloginfo('template_directory');?>/images/picturehere.png" alt=""></a>
            </h1>
    </div>
<?php } ?>
4

Same as Robert Hue's answer, this solution will get you the ID of the parent element:

echo get_post_field( 'post_parent' );

The second parameter is optional and can hold an ID (for when you are outside the loop or want to query the parent of a specific element)

$post_ID = 666;
echo get_post_field( 'post_parent', $post_ID );

This function can get you any field from the post table, like the post slug, status, type... see the docs for all available fields.

In your case the function would look like this:

if ( get_post_field( 'post_parent' ) === 9 ) :
    //child of page ID 9
endif;
2

Try this

if ( wp_get_post_parent_id( get_the_ID() ) != 0 ) {
    echo 'I am a child page';
    // your code
}
1

Use get_post_ancestors($post). It will return an array if the current shown post is child of your parent page.

2
  • 1
    Thanks for the quick response. Could you please clarify how I would implement this using or modifying the code I currently have? Commented Nov 24, 2014 at 3:15
  • I don't think this will solve the problem since this functions get_post_ancestors() only returns array of child page. It's good for checking if a page has child pages.
    – Robert hue
    Commented Nov 24, 2014 at 5:18
0

$post_current = get_post();

$args = array(
        'post_parent' => $parent_id, // the ID of the parent
        'posts_per_page' => -1,
        'post_type' => 'page',

);
$children = get_posts($args);

$is_child = false;

foreach ($children as $f) {
    if ( $f->ID == $post_current->post_parent ) {
        // it's a child 
        $is_child = true;
        break;
    }
}

if ($is_child) {
    // I'ts a child...
} else {
    // It is not a child...
}

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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