代码中的注意事项是由未定义的变量引起的。但还有一些事情可以做得更好一些。。。
function change_wp_title( $title ) {
global $post, $paged;
if ( is_404() ) { // move this check here; if it\'s 404, there is no point in checking parents, ancestors, and so on
$title[\'title\'] = \'file not available\';
return $title;
}
$parent_title = $parents_titles = \'\'; // initialise titles with empty string
// All parents.
$parents = get_post_ancestors( $post->ID );
if ( ! empty($parents) ) { // check if $parents is not empty
foreach ( array_reverse( $parents ) as $key => $parentpost ) {
$parents_titles .= get_post_field( \'post_title\', $parentpost ) . \' : \'; // You don\'t have to get all fields for given post
}
}
if ( $post->post_parent ) { // check if there is parent
$parent_title = get_the_title($post->post_parent) . \': \';
}
if ( 4808 == $post->post_parent ) {
$title[\'title\'] = \'About: Who: \' . $parent_title . $title[\'title\'];
}
// Various conditionals for other pages.
else {
$title[\'title\'] = $parents_titles . $title[\'title\'];
}
return $title;
}
add_filter( \'document_title_parts\', \'change_wp_title\', 20, 1);