如何对 Wordpress title-tag 部分进行排序?
How to sort Wordpress title-tag parts?
我需要更改标题部分的位置。现在我的主页呈现:
"blogname separator description"
我想把博客名放在最后:
"description separator blogname"
我的主题支持 title-tag
,我了解到它有 3 个过滤器,它们是:
add_theme_support( 'title-tag' );
pre_get_document_title
short-circuits wp_get_document_title()
如果它 returns 除了空值以外的任何值。
document_title_separator
过滤标题部分之间的分隔符。
document_title_parts
过滤构成文档标题的部分,传入关联数组。
如何使用它们来改变标题部分的位置?
更改主页上的标题部分或 front-page WordPress
@retroriff,如果您需要 title
的 re-order 和 tagline
作为首页 wp 标题的一部分,您可以使用过滤器document_title_parts
,并用一个函数触发。首先,我们需要取消设置它们,然后 re-build 作为顺序。这是我的示例方法:
add_filter( 'document_title_parts', 'wp36469459_document_title_parts', 1, 1 );
function wp36469459_document_title_parts( $title )
{
if ( is_home() || is_front_page() ) {
//we unset title and tagline
unset( $title['title'], $title['tagline'] );
//re-build and order
$title['tagline'] = get_bloginfo( 'description', 'display' );
$title['title'] = get_bloginfo( 'name', 'display' );
}
return $title;
}
或
add_filter( 'document_title_parts', 'wp36469459_document_title_parts', 1, 1 );
function wp36469459_document_title_parts( $title )
{
if ( is_home() || is_front_page() ) {
$_title = $title['title'];
$_tagline = $title['tagline'];
unset( $title['title'], $title['tagline'] );
$title['tagline'] = $_tagline;
$title['title'] = $_title;
}
return $title;
}
您可以根据需要调整代码。
如果您只需要反转标题部分(对于主页),那么这个可行:
function theme_document_title_parts($title) {
return (is_home() || is_front_page()) ? array_reverse($title) : $title;
}
add_filter('document_title_parts', 'theme_document_title_parts');
我需要更改标题部分的位置。现在我的主页呈现:
"blogname separator description"
我想把博客名放在最后:
"description separator blogname"
我的主题支持 title-tag
,我了解到它有 3 个过滤器,它们是:
add_theme_support( 'title-tag' );
pre_get_document_title
short-circuitswp_get_document_title()
如果它 returns 除了空值以外的任何值。document_title_separator
过滤标题部分之间的分隔符。document_title_parts
过滤构成文档标题的部分,传入关联数组。
如何使用它们来改变标题部分的位置?
更改主页上的标题部分或 front-page WordPress
@retroriff,如果您需要 title
的 re-order 和 tagline
作为首页 wp 标题的一部分,您可以使用过滤器document_title_parts
,并用一个函数触发。首先,我们需要取消设置它们,然后 re-build 作为顺序。这是我的示例方法:
add_filter( 'document_title_parts', 'wp36469459_document_title_parts', 1, 1 );
function wp36469459_document_title_parts( $title )
{
if ( is_home() || is_front_page() ) {
//we unset title and tagline
unset( $title['title'], $title['tagline'] );
//re-build and order
$title['tagline'] = get_bloginfo( 'description', 'display' );
$title['title'] = get_bloginfo( 'name', 'display' );
}
return $title;
}
或
add_filter( 'document_title_parts', 'wp36469459_document_title_parts', 1, 1 );
function wp36469459_document_title_parts( $title )
{
if ( is_home() || is_front_page() ) {
$_title = $title['title'];
$_tagline = $title['tagline'];
unset( $title['title'], $title['tagline'] );
$title['tagline'] = $_tagline;
$title['title'] = $_title;
}
return $title;
}
您可以根据需要调整代码。
如果您只需要反转标题部分(对于主页),那么这个可行:
function theme_document_title_parts($title) {
return (is_home() || is_front_page()) ? array_reverse($title) : $title;
}
add_filter('document_title_parts', 'theme_document_title_parts');