Create an if is_page statement based on parent page

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 ‘.com/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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Hey ! This should work.

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

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x