我正在使用此代码:
function the_alt_title($title= \'\') {
$page = get_page_by_title($title);
if ($p = get_post_meta($page->ID, "_x_entry_alternate_index_title", true)) {
$title = $p;
}
return $title;
}
add_filter(\'the_title\', \'the_alt_title\', 10, 1);
正在调试中。我得到的日志
PHP Notice: Trying to get property of non-object in /var/www/html/wp-content/themes/my-child/functions.php on this line:
if ($p = get_post_meta($page->ID, "_x_entry_alternate_index_title", true)) {
我怎样才能解决这个问题?
最合适的回答,由SO网友:ngearing 整理而成
$page = get_page_by_title($title)
- 该行在某个地方出现故障,因此您应该对此进行检查以确保它存在。
像这样:
function the_alt_title($title= \'\') {
$page = get_page_by_title($title);
if (!$page) {
return $title;
}
if ($p = get_post_meta($page->ID, "_x_entry_alternate_index_title", true)) {
$title = $p;
}
return $title;
}
add_filter(\'the_title\', \'the_alt_title\', 10, 1);