WordPress wp_索引页标题空白
我是WordPress的新手,刚刚安装了3.3.1版 我在谷歌上搜索了一下这个问题,找到了一些答案,但这些答案与2.7版相关,而且只有2-3岁 基本上,WordPress wp_索引页标题空白,wordpress,title,Wordpress,Title,我是WordPress的新手,刚刚安装了3.3.1版 我在谷歌上搜索了一下这个问题,找到了一些答案,但这些答案与2.7版相关,而且只有2-3岁 基本上,wp_title函数在每个页面上都可以正常工作,除了我的主页,它返回空白,我没有任何标题。我可以硬编码标题,但我不想这样做 罪行代码: <title><?php wp_title ( '| So Fresh n\' So Clean', true,'right' ); ?></title> 我在3.3.1
wp_title
函数在每个页面上都可以正常工作,除了我的主页,它返回空白,我没有任何标题。我可以硬编码标题,但我不想这样做
罪行代码:
<title><?php wp_title ( '| So Fresh n\' So Clean', true,'right' ); ?></title>
我在3.3.1中找不到与此问题相关的任何信息,因此很明显我做错了什么。以下是我的阅读: 如果您使用的是带有自定义循环的自定义主页,那么 将有一个空的
wp\u标题
。下面是一个添加
主页上wp\u标题处的说明/标语:
|
因此,使用is_front_page()
在主页上获取标题,这是上面代码中建议的方式。但是如果使用静态主页,这是代码:
<title><?php bloginfo('name'); ?> » <?php is_front_page() ? bloginfo('description') : wp_title(''); ?></title>
”;
根据Amna的答案,我想出了以下代码,当有页面标题时,应该显示页面标题,然后是站点名称
<?php wp_title(' - ',TRUE,'right'); bloginfo('name'); ?>
帖子/页面输出:“页面标题-站点名称”
主页输出:“站点名称”
显然,这也可以交换以首先显示站点名称
<?php bloginfo('name'); wp_title(' - '); ?>
帖子/页面输出:“站点名称-页面标题”
主页输出:“站点名称”
这也可以与条件组合在一起,以便在查看主页时显示站点描述
<?php bloginfo('name'); echo ' - '; is_front_page() ? bloginfo('description') : wp_title(''); ?>
帖子/页面输出:“站点名称-页面标题”
主页输出:“站点名称-站点描述”我使用这个,它从未失败过:
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;
}
请注意,其中有一些翻译域可能需要更改。对于wordpress wp\u title empty上的google搜索,这是第一个结果。因此,我想我可以分享最优雅的解决方案。
在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 );
来自Codex的新攻击如下:
<title><?php wp_title(''); ?></title>
我在我的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;
谈话迟到了
但是,如果要使用静态首页所用页面的实际标题,可以使用以下内容:
if (is_front_page())
{
$title = single_post_title( '', false );
}
尽管在wp_title()的实际源中,存在以下情况,特别是在静态首页禁用此功能:
if ( is_single() || ( is_home() && ! is_front_page() ) || ( is_page() && ! is_front_page() ) ) {
$title = single_post_title( '', false );
}
我想这是有充分理由的。因此,请谨慎行事。更新
对于WordPress版本(>=4.4)
试试这个
function some_name(){
add_theme_support( 'title-tag' );
}
add_action( 'after_setup_theme', 'some_name' );
在functions.php中执行此操作,并删除头部的“title”标记…我的“misty lake”主题2美分,该主题在主页上没有标题,在所有其他页面上添加了不正确的标题
只需从header.php中删除以下行就可以解决这个问题,因为Wordpress现在自己注入标记:
<title><?php wp_title( '|', true, 'right' ); ?></title>
我查阅了以下页面–您也可以在您的标题标签中添加类似的内容:
<?php
if (is_front_page()) { ?>
Home | <?php bloginfo('description');
} else {
wp_title('|', 'true', 'right'); bloginfo('description');
} ?>
家|
不需要。只需将
代码添加到头文件的末尾。php
祝你好运。首选使用is_首页,而不是is_主页is_首页,因为它会更通用。现在,该链接已更改,但您的答案仍然有效,但已过时。有关更新WP版本(>=4.4)的更好解决方案,请参阅waLL e的答案显然,对于儿童主题,这是一个比公认答案更合适、更有用的代码。只需将其放在子主题的functions.php中,这似乎是更新版本WP的更正确答案。我从模板中删除了标签,标题标签出现在主页上。在4.4中声明为不推荐。然而,这是一个“不推荐的”,很好的解决方案,它也可以更好地与像Yoast SEO这样的插件配合使用,从而设置标题。你的答案是完美的
<title><?php wp_title( '|', true, 'right' ); ?></title>
<?php
if (is_front_page()) { ?>
Home | <?php bloginfo('description');
} else {
wp_title('|', 'true', 'right'); bloginfo('description');
} ?>