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 badges1 Answer
Reset to default 0Hey ! This should work.
$current_url = home_url( $wp->request );
if (strpos($current_url,'courses') !== false)
{
//Do Something
}
else
{
//Do Something
}