post_id always wrong in plugin

You can’t use get_the_ID() when you are not in the loop (eg. in nav menus). Fortunately, the the_title filter comes with the post ID in an extra argument.

Change your code to this, to make it work in all places:

function mealingua_title_filter($title, $post_id) {

  #some stuff
  if (is_front_page() OR is_single() OR is_page()) {
    return '<span class="mealingua_title_container_' . $post_id . ' mealingua_title_container">' . $title . '</span>';
  } else {
    return $title;
  }
}

add_filter('the_title', 'mealingua_title_filter', 10, 2);

Leave a Comment