1

I've inherited a WordPress project with a heavily customised theme. Currently have an issue when viewing the blog when there are no current post entries. Within the theme code, there is an include to a file named submenu.php which will give a customised menu according to the page child that you are on (like this question illustrates https://wordpress.org/support/topic/how-to-get-top-level-parent-pages/). This is a global throughout the theme and is called on all pages. If no parents are found, it just does not display a submenu.

My issue is that it conflicts with the defined blogroll page and throws up

"Notice: Trying to get property of non-object in {file location} on line 31

The offending code is thus:

/**
 * Get the top level page in the tree for the current page
 **/
function get_top_level_page() {
  global $post;

  $parent = array_reverse(get_post_ancestors($post->ID));
  $first_parent;

  if (!empty($parent)) {
      $first_parent = get_page($parent[0]);
      // echo '1'. $first_parent->post_name;
  } else if (get_post_type() == 'page') {
      $first_parent = $post;
      // echo '2'. $first_parent->post_name;
  } else {
    // If we're in a custom post type build from the slug page
    $post_type = get_post_type_object(get_post_type());
    $slug = $post_type->rewrite;

    $slugPages = explode('/', $slug['slug']);
    $first_parent = get_page_by_path($slugPages[0]);
    $last_page = get_page_by_path($slug['slug']);

    if (is_home() && get_option('page_for_posts')) {
      $first_parent = get_page( get_option('page_for_posts'));
    }
  }
  return $first_parent;
}

From what I can gather, it cannot find a value for $parent = array_reverse(get_post_ancestors($post->ID));

Also the heuristic defaults to the "else" option and then looks for the parent. I have put in some extra code to detect if $parent is empty - that appears to work and avoid the else statement.

However there are other areas where the parent values come up and I think it's best that the blog page does not even call the submenu.php script. Where should I go looking to find what sets the template for the blog page? I have a feeling that I could block the submenu.php script from loading, I should not even have this issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.