admin管理员组文章数量:1289402
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="">
<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'?
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">
<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'?
Share Improve this question edited Nov 24, 2014 at 5:04 Robert hue 8,5562 gold badges34 silver badges50 bronze badges asked Nov 24, 2014 at 2:59 user1752759user1752759 6673 gold badges10 silver badges18 bronze badges7 Answers
Reset to default 16You 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 }
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.
<?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 } ?>
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;
Try this
if ( wp_get_post_parent_id( get_the_ID() ) != 0 ) {
echo 'I am a child page';
// your code
}
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... }
本文标签: Check if is on childpage of a particular page
版权声明:本文标题:Check if is on child-page of a particular page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741423114a2377920.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论