WordPress wp_title 索引页上为空白

发布于 2024-12-29 18:53:26 字数 371 浏览 1 评论 0 原文

我是 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 中发生的这个问题的信息,所以很明显我做错了什么。

I'm new to WordPress and just installed version 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.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

姐不稀罕 2025-01-05 18:53:26

以下是我从 Codex 中读到的内容:

如果您使用带有自定义循环和内容的自定义主页,您
将有一个空的 wp_title。这是一个巧妙的技巧来添加
主页 wp_title 处的描述/标语:

<title><?php bloginfo('name'); ?> | <?php is_front_page() ? bloginfo('description') : wp_title(''); ?></title>

因此,请使用 is_front_page() 获取主页上的标题,如上面代码中建议的方式。

Here's is what I read from 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 the wp_title place on homepage:

<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.

别闹i 2025-01-05 18:53:26

更新
对于 WordPress 版本 (>= 4.4)

试试这个

function some_name(){
    add_theme_support( 'title-tag' );
}

add_action( 'after_setup_theme', 'some_name' );

在functions.php 中执行此操作并从 head 中删除“title”标签...

Update
for WordPress versions (>= 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...

等你爱我 2025-01-05 18:53:26

但如果您使用静态主页,则代码如下:

<title><?php bloginfo('name'); ?> » <?php is_front_page() ? bloginfo('description') : wp_title(''); ?></title>

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>
蓝梦月影 2025-01-05 18:53:26

根据 Amna 的回答,我想出了以下代码,该代码应该在有页面标题时显示页面标题,后跟网站名称。

<?php wp_title(' - ',TRUE,'right'); bloginfo('name'); ?>

帖子/页面输出:“页面标题 - 站点名称”

主页输出:“站点名称”


显然,也可以将其交换为首先显示站点名称。

<?php bloginfo('name'); wp_title(' - '); ?>

帖子/页面输出:“站点名称 - 页面标题”

主页输出:“站点名称”


这也可以与条件结合使用,以在查看主页时显示站点描述。

<?php bloginfo('name'); echo ' - '; is_front_page() ? bloginfo('description') : wp_title(''); ?>

帖子/页面输出:“站点名称 - 页面标题”

主页输出:“站点名称 - 站点描述”

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.

<?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"

一袭水袖舞倾城 2025-01-05 18:53:26

对于在 wordpress wp_title 上的 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 );

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.

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 );
葬花如无物 2025-01-05 18:53:26

Codex 的新 hack 如下:

<title><?php wp_title(''); ?></title>

然后在主题文件的“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;
}

The new hack from Codex is as follows:

<title><?php wp_title(''); ?></title>

Then in your "functions.php" from theme file :

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;
}
坠似风落 2025-01-05 18:53:26

我使用这个,它从未失败过:

    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;
    }

请注意,其中有您可能想要更改的翻译域。

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.

呆萌少年 2025-01-05 18:53:26

我的“迷雾湖”主题 2 美分,该主题在主页上没有标题,并且在所有其他页面上添加了错误的标题。

只需从 header.php 中删除以下行即可解决问题,因为 Wordpress 现在自行注入标签:

<title><?php wp_title( '|', true, 'right' ); ?></title>

我查阅了以下页面 – https://make.wordpress.org/themes/2015/08/25/title-tag-support-now-required/

My 2 cents for "misty lake" theme which had no title on home page and added incorrect title on all other pages.

Just removing the following line from header.php solves the issue, since Wordpress now injects the tag by itself:

<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/

深居我梦 2025-01-05 18:53:26

不需要。只需添加 代码位于 header.php 末尾,

祝你好运。

no need. Just add the <? Php wp_head ();?> Code at the end of the header.php

good luck.

灯下孤影 2025-01-05 18:53:26

我在我的 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;

I use this method in my WordPress site

//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;
狂之美人 2025-01-05 18:53:26

对话晚了...

但是如果您想使用用于静态首页的页面的实际标题,您可以使用以下内容:

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 );
}

我怀疑这样做是有充分理由的。因此,请谨慎行事。

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:

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.

尬尬 2025-01-05 18:53:26

您还可以在标题标签中添加类似的内容:

<?php 
    if (is_front_page()) { ?> 
        Home | <?php bloginfo('description'); 
    } else {
        wp_title('|', 'true', 'right'); bloginfo('description');
    } ?>

You could also put something like this inside your title tag:

<?php 
    if (is_front_page()) { ?> 
        Home | <?php bloginfo('description'); 
    } else {
        wp_title('|', 'true', 'right'); bloginfo('description');
    } ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文