I’m trying to display content based on the current WordPress page or subpage, working with page titles instead of IDs.
What I have is this:
<?php if ( is_page( array( 'About', 'Chi Siamo' ) ) || ( get_the_title( $post->post_parent ) == ( array( 'About', 'Chi Siamo' ) ) ) ) { ?>
<p>Hello Joe</p>
<?php } else {} ?>
Not sure why, but it’s working on identifying the page names, but not the subpages of that page.
Can anyone see what I may have done wrong? Syntax error?
Thanks
PS. I have also tried
<?php if (get_the_title() || get_the_title( $post->post_parent ) == array( 'About', 'Chi Siamo' ) ) { ?>
But this returns true for all pages.
I would remove the outer parentheses from the second part of your conditional:
( get_the_title( $post->post_parent ) == ( array( 'About', 'Chi Siamo' ) ) )and make sure$postis actually what you think it is.Try to
var_dump($post)and see ifpost_parentis actually present.It’s ugly to check for pages by title (slug or id would really be preferable), especially when it comes to finding their ancestors. The beauty of PHP is that it is dynamic, and you wont need to be manually changing IDs with each theme install. Why not get the ID of the current page and then pass it to get_ancestors?
Store that array in a variable, maybe
$ancestors, and then$ancestors[0]will be the ID of the parent page.get_the_titleof that ID, and then check if that is ‘About’ or ‘Chi Siamo’.Update:
Here’s my previous answer in copyable code form:
and then your conditional