WordPress wp_title 在索引页面上是空白的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9055009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WordPress wp_title blank on index page
提问by nmford
I'm new to WordPress and just installed version 3.3.1.
我是 WordPress 的新手,刚刚安装了 3.3.1 版。
I did some googling regarding this question and found some answers but they were relevant to version 2.7 and were 2-3 years old.
我对这个问题进行了一些谷歌搜索并找到了一些答案,但它们与 2.7 版相关并且已经使用了 2-3 年。
Basically, the wp_title
function works fine on every page except my home page where it returns blank and I get no title whatsoever. I could just hard code the title in but I'd rather not do that.
基本上,wp_title
除了我的主页返回空白并且我没有任何标题外,该功能在每个页面上都可以正常工作。我可以硬编码标题,但我宁愿不这样做。
Guilty line of code:
有罪的代码行:
<title><?php wp_title ( '| So Fresh n\' So Clean', true,'right' ); ?></title>
I couldn't find anything regarding this problem happening in 3.3.1 so clearly I've done something wrong.
我找不到关于 3.3.1 中发生的这个问题的任何信息,所以很明显我做错了什么。
回答by Amna Ahmed
Here's is what I read from Codex:
这是我从Codex 中读到的内容:
If you are using a custom homepage with custom loops and stuff, you will have an empty
wp_title
. Here goes a neat hack to add the description/tagline at thewp_title
place on homepage:
如果您使用带有自定义循环和内容的自定义主页,您将有一个空的
wp_title
. 这是一个巧妙的技巧,可以wp_title
在主页上的位置添加描述/标语:
<title><?php bloginfo('name'); ?> | <?php is_front_page() ? bloginfo('description') : wp_title(''); ?></title>
So use is_front_page()
to get the title on homepage, the way it is suggested in above code.
所以使用is_front_page()
上面代码中建议的方式获取主页上的标题。
回答by danielgc
But if you use a static home page, this is the code:
但是,如果您使用静态主页,则代码如下:
<title><?php bloginfo('name'); ?> » <?php is_front_page() ? bloginfo('description') : wp_title(''); ?></title>
回答by w411 3
Updatefor WordPress versions (>= 4.4)
WordPress 版本更新(>= 4.4)
Try this
尝试这个
function some_name(){
add_theme_support( 'title-tag' );
}
add_action( 'after_setup_theme', 'some_name' );
Do this in functions.php and remove 'title' tag from head...
在functions.php中执行此操作并从head中删除'title'标签...
回答by ian.pvd
Working off of Amna's answer, I came up with the following code which should display the page title when there is one, followed by the site name.
根据 Amna 的回答,我想出了以下代码,当有页面标题时,它应该显示页面标题,然后是站点名称。
<?php wp_title(' - ',TRUE,'right'); bloginfo('name'); ?>
Post/Page Outputs: "The Page Title - Site Name"
帖子/页面输出:“页面标题 - 站点名称”
Home Page Outputs: "Site Name"
主页输出:“站点名称”
Obviously, this can also be swapped to display the site name first.
显然,这也可以交换为首先显示站点名称。
<?php bloginfo('name'); wp_title(' - '); ?>
Post/Page Outputs: "Site Name - The Page Title"
帖子/页面输出:“站点名称 - 页面标题”
Home Page Outputs: "Site Name"
主页输出:“站点名称”
This can also be combined with a conditional to display the site description when viewing the home page.
这也可以与条件结合以在查看主页时显示站点描述。
<?php bloginfo('name'); echo ' - '; is_front_page() ? bloginfo('description') : wp_title(''); ?>
Post/Page Outputs: "Site Name - The Page Title"
帖子/页面输出:“站点名称 - 页面标题”
Home Page Outputs: "Site Name - The Site Description"
主页输出:“站点名称 - 站点描述”
回答by BogdanD
For google search on wordpress wp_title empty this is the first result. So I thought that I might share the most elegant solution for this.
In functions.php add a filter for wp_title.
对于 wordpress wp_title 空的谷歌搜索,这是第一个结果。所以我想我可能会为此分享最优雅的解决方案。
在functions.php 中为wp_title 添加一个过滤器。
function custom_wp_title( $title, $sep ) {
if ( is_feed() ) {
return $title;
}
global $page, $paged;
// Add the blog name
$title .= get_bloginfo( 'name', 'display' );
// Add the blog description for the home/front page.
$site_description = get_bloginfo( 'description', 'display' );
if ( $site_description && ( is_home() || is_front_page() ) ) {
$title .= " $sep $site_description";
}
// Add a page number if necessary:
if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
$title .= " $sep " . sprintf( __( 'Page %s', '_s' ), max( $paged, $page ) );
}
return $title;
}
add_filter( 'wp_title', 'custom_wp_title', 10, 2 );
回答by ed-ta
The new hack from Codex is as follows:
Codex 的新 hack 如下:
<title><?php wp_title(''); ?></title>
Then in your "functions.php" from theme file :
然后在主题文件中的“functions.php”中:
add_filter( 'wp_title', 'baw_hack_wp_title_for_home' );
function baw_hack_wp_title_for_home( $title )
{
if( empty( $title ) && ( is_home() || is_front_page() ) ) {
return __( 'Home', 'theme_domain' ) . ' | ' . get_bloginfo( 'description' );
}
return $title;
}
回答by pie6k
I use this one and it never failed:
我使用了这个,它从未失败过:
function pageTitle($echo){
$title = "";
global $paged;
if (function_exists('is_tag') && is_tag()) {
$title .= single_tag_title(__("Tag Archive for "" , 'circle'),false);
$title .= '" - ';
}
elseif (is_archive()) {
$title .= wp_title('',true);
//$title .= __(' Archive - ' , 'circle');
$title .= __(' - ' , 'circle');
}
elseif (is_search()) {
$title .= __('Search for "' , 'circle') . esc_html(get_search_query()).'" - ';
}
elseif (!(is_404()) && (is_single()) || (is_page())) {
$title .= wp_title('',true);
$title .= ' - ';
}
elseif (is_404()) {
$title .= __('Not Found - ' , 'circle');
}
if (is_home()) {
$title .= get_bloginfo('name');
$title .= ' - ';
$title .= get_bloginfo('description');
}
else {
$title .= get_bloginfo('name');
}
if ($paged>1) {
$title .= ' - page ' . $paged;
}
if ( !$echo ) return $title;
echo $title;
}
Note that there are translation domains in it that you might want to change.
请注意,其中有您可能想要更改的翻译域。
回答by Adel
I use this method in my WordPress site
我在我的 WordPress 网站中使用这种方法
//Meta Header
if ( ! function_exists( 'dima_wp_title' ) ) :
function dima_wp_title( $title ) {
if ( is_front_page() ) {
return get_bloginfo( 'name' ) . ' | ' . get_bloginfo( 'description' );
} elseif ( is_feed() ) {
return ' | RSS Feed';
} else {
return trim( $title ) . ' | ' . get_bloginfo( 'name' );
}
}
add_filter( 'wp_title', 'dima_wp_title' );
endif;
回答by Jahmic
Late to the conversation...
谈话迟到...
But if you want to use the actual title of the page that you are using for the static front page, you can use the following:
但是,如果要使用用于静态首页的页面的实际标题,则可以使用以下内容:
if (is_front_page())
{
$title = single_post_title( '', false );
}
Although, in the actual source for wp_title(), there is the following, specificaly disabling this for the static front page:
虽然,在 wp_title() 的实际源代码中,有以下内容,专门为静态首页禁用此功能:
if ( is_single() || ( is_home() && ! is_front_page() ) || ( is_page() && ! is_front_page() ) ) {
$title = single_post_title( '', false );
}
I suspect there is good reason for this. So, proceed with caution.
我怀疑这是有充分理由的。因此,请谨慎行事。
回答by Misha Reyzlin
My 2 cents for "misty lake" theme which had no title on home page and added incorrect title on all other pages.
我的 2 美分“迷雾湖”主题在主页上没有标题,并在所有其他页面上添加了错误的标题。
Just removing the following line from header.php solves the issue, since Wordpress now injects the tag by itself:
只需从 header.php 中删除以下行即可解决问题,因为 Wordpress 现在自行注入标签:
<title><?php wp_title( '|', true, 'right' ); ?></title>
I consulted the following page –?https://make.wordpress.org/themes/2015/08/25/title-tag-support-now-required/
我查阅了以下页面——?https://make.wordpress.org/themes/2015/08/25/title-tag-support-now-required/