最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Create an if is_page statement based on parent page

programmeradmin2浏览0评论

I am trying to show something in a template based on what page is showing. This is working fine until I want to show a part of the template based on whether the parent post page slug is 'courses'.

So the page url ends in '/courses/course-1' so I want specific code to show for all posts where the post parent slug is 'courses'

I have tried the following:

global $post;
$post_data = get_post($post->post_parent);

<?php if ( is_page( array( 'learn', 'profile') )  || ($post_data->post_parent == 'courses') ) { ?>

THINGS GO HERE 

<?php } ?>

but for some reason once I add the code || ($post_data->post_parent == 'courses') the if statement is then ignored on the template. The result is that even if a page not listed in the array, the 'THINGS GO HERE' is generated anyway.

What am I doing wrong?

I am trying to show something in a template based on what page is showing. This is working fine until I want to show a part of the template based on whether the parent post page slug is 'courses'.

So the page url ends in '/courses/course-1' so I want specific code to show for all posts where the post parent slug is 'courses'

I have tried the following:

global $post;
$post_data = get_post($post->post_parent);

<?php if ( is_page( array( 'learn', 'profile') )  || ($post_data->post_parent == 'courses') ) { ?>

THINGS GO HERE 

<?php } ?>

but for some reason once I add the code || ($post_data->post_parent == 'courses') the if statement is then ignored on the template. The result is that even if a page not listed in the array, the 'THINGS GO HERE' is generated anyway.

What am I doing wrong?

Share Improve this question asked Nov 30, 2020 at 5:03 Hana FejzicHana Fejzic 132 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Hey ! This should work.

$current_url = home_url( $wp->request );

if (strpos($current_url,'courses') !== false)
{
    //Do Something
}
else
{
    //Do Something
}
发布评论

评论列表(0)

  1. 暂无评论