首页 文章

WordPress wp_title在索引页面上留空

提问于
浏览
36

我是WordPress的新手,刚刚安装了3.3.1版本 .

我做了一些关于这个问题的谷歌搜索,并找到了一些答案,但它们与版本2.7相关,并且是2-3岁 .

基本上, wp_title 函数在每个页面上工作正常,除了我的主页,它返回空白,我没有得到任何 Headers . 我可以硬编码 Headers ,但我宁愿不这样做 .

有罪的代码行:

<title><?php wp_title ( '| So Fresh n\' So Clean', true,'right' ); ?></title>

我找不到有关3.3.1中发生的这个问题的任何事情,所以很清楚我做错了什么 .

11 回答

  • 9

    Update for WordPress版本(> = 4.4)

    试试这个

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

    在functions.php中执行此操作并从头部删除“title”标记...

  • 7

    这是我从Codex读到的内容:

    如果您使用带有自定义循环和内容的自定义主页,则会有一个空的wp_title . 这里有一个简洁的黑客在主页上的wp_title位置添加描述/标语:

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

    因此,使用 is_front_page() 获取主页上的 Headers ,就像上面代码中的建议方式一样 .

  • 13

    但是如果你使用静态主页,这就是代码:

    <title><?php bloginfo('name'); ?> &raquo; <?php is_front_page() ? bloginfo('description') : wp_title(''); ?></title>
    
  • 0

    根据Amna的回答,我想出了以下代码,当有一个代码时,它应该显示页面 Headers ,然后是网站名称 .

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

    发布/页面输出:“页面 Headers - 站点名称”

    主页输出:“站点名称”


    显然,这也可以交换显示网站名称 .

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

    发布/页面输出:“站点名称 - 页面 Headers ”

    主页输出:“站点名称”


    这也可以与条件组合以在查看主页时显示站点描述 .

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

    发布/页面输出:“站点名称 - 页面 Headers ”

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

  • 3

    对于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 );
    
  • 0

    我使用这个,它永远不会失败:

    function pageTitle($echo){
            $title = "";
    
            global $paged;
            if (function_exists('is_tag') && is_tag()) {        
                $title .= single_tag_title(__("Tag Archive for &quot;" , 'circle'),false); 
                $title .= '&quot; - '; 
            }
            elseif (is_archive()) {
                $title .= wp_title('',true); 
                //$title .= __(' Archive - ' , 'circle');
                $title .= __(' - ' , 'circle');
    
            }
            elseif (is_search()) {
            $title .= __('Search for &quot;' , 'circle') . esc_html(get_search_query()).'&quot; - '; 
            }
            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.

  • 0

    Codex的新黑客如下:

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

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

    谈话的后期......

    但是,如果要使用用于静态首页的页面的实际 Headers ,可以使用以下内容:

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

    我怀疑这是有充分理由的 . 所以,请谨慎行事 .

  • 1

    我的2美分“雾湖”主题在主页上没有 Headers ,并在所有其他页面上添加了错误的 Headers .

    只是从header.php中删除以下行解决了这个问题,因为Wordpress现在自己注入标记:

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

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

  • 0

    你也可以在你的 Headers 标签中加入这样的东西:

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

相关问题