Check if is on child-page of a particular page

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'?

Topic child-pages pages Wordpress

Category Web


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.


Try this

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

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;

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



$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...
}


<?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 } ?>

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 }

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.