您已经提到了函数wp_title()
. 在输出标题之前,它会将其值通过过滤器wp_title
, 这里可以使用它来预先添加其他信息。
add_filter(\'wp_title\', \'WPSE_20181106_prepend_title\', 10, 3);
function WPSE_20181106_prepend_title($title, $sep, $seplocation) {
// not a single post
if (!is_singular(\'post\')) {
return $title;
}
// IDs of categories that should prepend the title
$prepend_categories = [15, 35];
// get all categories of post
$categories = get_the_category();
foreach ($categories as $category) {
// found category
if (in_array($category->term_id, $prepend_categories)) {
// return new format, using __() so it is translateable
return sprintf(\'%s %s: %s\',
__(\'Download\', \'lang-slug\'),
$category->name,
$title
);
}
}
// category not found, return default
return $title;
}