Support » Fixing WordPress » Checking if a page is a child page not taking effect.

  • isitegate

    (@isitegate)


    Hi all, I have this snippet of code:

    <?php 
      global $post; // if outside the loop
    	if ( is_page() && $post->post_parent ) {
       	// This should be a subpage
    ?>
    // HTML code //
    <?php 
     }  
    ?>

    the HTML code doesn’t appear on the pages at all no matter what file i place the code in, page.php, footer.php, etc..
    But ..
    If i just inject the html code without the condition it appears in frontend straightaway without any problem.
    Any reason or any mistakes in my code above?

    Thanks a lot in advance.

    The page I need help with: [log in to see the link]

Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Forum Moderator

    Try if ( is_page() && ( $post->post_parent != 0 ) ) {

    if that doesn’t work, do a print_r($post) to see what’s in the current post object.

    fullworks

    (@fullworks)

    Hi,

    I have put your code into page.php of twentytwenty
    as follows and it works perfectly

    If it is a sub page it displays it is a sub page – if not it doesn’t

    hope that helps you sort out your actually issue

    * The template for displaying all single posts
     *
     * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
     *
     * @package WordPress
     * @subpackage Twenty_Twenty_One
     * @since 1.0.0
     */
    
    get_header();
    
    global $post; // if outside the loop
    if ( is_page() && $post->post_parent ) {
    // This should be a subpage
    ?>
    This is a sub page
    <?php
    }
    
    /* Start the Loop */
    
    Thread Starter isitegate

    (@isitegate)

    @fullworks: Thanks a lot.

    @sterndata: it worked for first level children but not below that, and i have printed the current post object but can’t understand it.. please have a look

    this is home page :

    Home [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => temporary-staff [to_ping] => [pinged] => [post_modified] => 2020-11-21 14:25:19 [post_modified_gmt] => 2020-11-21 14:25:19 [post_content_filtered] => [post_parent] => 0 [guid] => https://temporary-staff.agency/?page_id=15 [menu_order] => 0 [post_type] => page [post_mime_type] => [comment_count] => 0 [filter] => raw )
    
    WP_Post Object ( [ID] => 15 [post_author] => 1 [post_date] => 2016-03-11 12:27:10 [post_date_gmt] => 2016-03-11 12:27:10 [post_content] =>
    TEMPORARY STAFF AGENCY

    And this is a second level child page:

     [post_title] => Dental Nurse Agency [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => dental-nurse-agency [to_ping] => [pinged] => [post_modified] => 2020-07-24 18:24:46 [post_modified_gmt] => 2020-07-24 18:24:46 [post_content_filtered] => [post_parent] => 0 [guid] => https://temporary-staff.agency/dental-nurse-agency/ [menu_order] => 0 [post_type] => page [post_mime_type] => [comment_count] => 0 [filter] => raw )
    
    WP_Post Object ( [ID] => 1334 [post_author] => 1 [post_date] => 2020-07-24 18:24:46 [post_date_gmt] => 2020-07-24 18:24:46 [post_content] =>
    
    Thread Starter isitegate

    (@isitegate)

    please can you help my fixing my issue?
    or at least.. can i ask for another help please?

    I now can add the HTML without any logic then in the custom CSS i can hide it on partent pages.
    This can be easy if i have something like <body clas=” page page-id-.. “> but in my them all pages have the same body tag attributes, there is a different class assigned to the html tage but that didn’t work, please see screenshot https://prnt.sc/x8fwcu

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Forum Moderator

    This is the body for a child page:

    <body class="page-template-default page page-id-1133 page-child parent-pageid-1725 logged-in admin-bar no-customize-support wp-custom-logo wp-embed-responsive is-light-theme no-js singular has-main-navigation">
    

    and this is the body for its parent

    <body class="page-template-default page page-id-1725 page-parent logged-in admin-bar no-customize-support wp-custom-logo wp-embed-responsive is-light-theme no-js singular has-main-navigation">
    

    Note the classes “page-child” and “page-parent”. You can use those for styling.

    These should be added in the normal course of events. If the classes are not there, then your theme is an issue.

    Pages in question: https://bertha.sterndata.com/about/ and https://bertha.sterndata.com/about/page-image-alignment/

    WordPress 5.6, theme is twentytwentyone

    Thread Starter isitegate

    (@isitegate)

    No those classes are not there Steve,
    neither logical condition nor styling is working for me

    Thread Starter isitegate

    (@isitegate)

    your peice of code worked for first level child page and not below that.. any hint can be taken from that?

    fullworks

    (@fullworks)

    Your theme appears to be built in a very different way to most, it appears to be totally dependent on javascript with no fallback which means – unless you tweak some code you are unlikely to be able to use css.

    In your example about your second level child page: is simply not a child page as it – post_parent = 0 is a top level page.

    Do you have a screen shot of page editor of the child page showing it has a parent page?

    • This reply was modified 2 years ago by fullworks.
    • This reply was modified 2 years ago by fullworks.
    Moderator Steven Stern (sterndata)

    (@sterndata)

    Forum Moderator

    It appears that the fiverr person you hired to build your site’s theme does not use the body_class function properly. You might go back to them and ask them to add it. For an example, see https://github.com/WordPress/twentytwentyone/blob/trunk/header.php#L23

    Thread Starter isitegate

    (@isitegate)

    @fullworks;
    Thank you very much, i have checked and it is set as has no parent.
    I will change that now and try the code again.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Checking if a page is a child page not taking effect.’ is closed to new replies.