我用这个functions.php
输出我的页面<title>
:
/**
* Creates a nicely formatted and more specific title element text
* for output in head of document, based on current view
*
* @param string $title Default title text for current view
* @param string $sep Optional separator
* @return string Filtered title
*/
function sitename_wp_title( $title, $sep ) {
global $paged, $page;
if ( is_feed() )
return $title;
// Adds the site name
$title .= get_bloginfo( \'name\' );
// Adds the site description for the front page
$site_description = get_bloginfo( \'description\', \'display\' );
if ( $site_description && ( is_front_page() ) )
$title = "$title $sep $site_description";
// Adds a page number if necessary
if ( $paged >= 2 || $page >= 2 )
$title = "$title $sep " . sprintf( __( \'Page %s\' ), max( $paged, $page ) );
return $title;
}
add_filter( \'wp_title\', \'sitename_wp_title\', 10, 2 );
我想包括
top level category name 在子页上。
例如,当前具有以下站点结构:
“大”类别中的帖子将输出如下页面标题:
<title>$postname | $blog_name</title>
我希望输出为:
<title>$postname | Work | $blog_name</title>
因此会添加顶级类别名称,但不会添加二级类别(大型)。
最合适的回答,由SO网友:fuxia 整理而成
创建助手函数以获取所有父类别(每个帖子可以包含多个类别):
function parent_cat_names( $sep = \'|\' )
{
if ( ! is_single() or array() === $categories = get_the_category() )
return \'\';
$parents = array ();
foreach ( $categories as $category )
{
$parent = end( get_ancestors( $category->term_id, \'category\' ) );
if ( ! empty ( $parent ) )
$top = get_category( $parent );
else
$top = $category;
$parents[ $top->term_id ] = $top;
}
return esc_html( join( $sep, wp_list_pluck( $parents, \'name\' ) ) );
}
将父术语添加为…
if ( \'\' !== $parent_cats = parent_cat_names( $sep ) )
$title .= "$parent_cats $sep ";