PHP Notices appear when browsing any page in admin, but only for child theme, using code from WP Codex

My php_error.log in my console (locally on OSX) is reporting 3 PHP Notices. The PHP Notices only appear when browsing each admin page in the back-end for a site (running multisite) that uses a child theme. The child theme uses the parent theme’s sidebar.php which the code below is placed in.

The PHP notices do not show for the site that uses the actual parent theme. Also, if I make another sidebar.php in the child theme, then the notices are still present. Interestingly, these notices do not appear when browsing the site locally, and everything works as expected. They only appear when browsing around the admin.
The code in my sidebar is the exact code from this page on the WP Codex.

Read More

The exact code is:

<?php
 if($post->post_parent) {
 $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
 $titlenamer = get_the_title($post->post_parent);
}

else {
 $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
 $titlenamer = get_the_title($post->ID);
}

if ($children) { ?>

<h2> <?php echo $titlenamer; ?> </h2>
<ul>
  <?php echo $children; ?>
</ul>

<?php } ?>

Any the PHP notices are:

PHP Notice:  Trying to get property of non-object in sidebar.php on line 10
PHP Notice:  Trying to get property of non-object in sidebar.php on line 16
PHP Notice:  Trying to get property of non-object in sidebar.php on line 17

The lines in question from the above code are:

if($post->post_parent) { // line 10
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0"); // line 16
$titlenamer = get_the_title($post->ID); // line 17

Any ideas why these notices would be appearing in the admin pages and only for the child theme? Should I just ignore them?

Related posts

Leave a Reply

1 comment

  1. That’s pretty simple:

    You’re trying to get the parent property from the $post object, but that is either NULL or simply not attached. In both cases, you are

    Trying to get property of non-object
    

    from the $post object.

    Simply check if ( isset( $post->parent ) ) { /* do stuff */ }.