最合适的回答,由SO网友:Frank P. Walentynowicz 整理而成
下面的代码假定只有一个标记(“private”)具有与其关联的文本:
function add_tag_text_to_title( $title, $id = null ) {
if ( has_tag( \'private\' ) ) {
return $title . \' - Awesome\';
} else {
return $title;
}
}
add_filter( \'the_title\', \'add_tag_text_to_title\', 10, 2 );
如果您有多个标记及其关联的文本:
function add_tag_texts_to_title( $title, $id = null ) {
$tag_texts = array (
\'tag1\' => \'text1\',
\'tag2\' => \'text2\',
\'tag3\' => \'text3\'
);
$new_title = $title;
foreach ( $tag_texts as $key => $value ) {
if ( has_tag( $key ) )
$new_title .= \' - \' . $value;
}
return $new_title;
}
add_filter( \'the_title\', \'add_tag_texts_to_title\', 10, 2 );