【问题标题】:Wordpress HTML title is messed upWordpress HTML 标题搞砸了
【发布时间】:2012-12-23 02:55:41
【问题描述】:

我无法让我的自定义 WordPress 主题中的标题正确显示。标题属性设置如下:<title><?php wp_title( '|', true, 'right' ); ?></title>

我直接从 2012 主题中获取了该代码。当我激活 2012 主题时,标题看起来很完美,但是当我使用上面相同代码的自定义主题时,我将我的网站地址作为主页上的标题,而在其他每个页面上,我只得到页面名称后跟一个“|”。知道是什么原因造成的吗?我已经寻找了第二个标题标签,但到目前为止还没有。它破坏了我的 SEO。

将此添加到我的functions.php:

function twentytwelve_wp_title( $title, $sep ) {
    global $paged, $page;

    if ( is_feed() )
        return $title;

    // Add the site name.
    $title .= get_bloginfo( 'name' );

    // Add the site description for the home/front page.
    $site_description = get_bloginfo( 'description', 'display' );
    if ( $site_description && ( is_home() || is_front_page() ) )
        $title = "$title $sep $site_description";

    // Add a page number if necessary.
    if ( $paged >= 2 || $page >= 2 )
        $title = "$title $sep " . sprintf( __( 'Page %s', 'twentytwelve' ), max( $paged, $page ) );

    return $title;
}
add_filter( 'wp_title', 'twentytwelve_wp_title', 10, 2 );

【问题讨论】:

  • 您是否阅读了整个页面codex.wordpress.org/Function_Reference/wp_title
  • 如果您发布屏幕截图或页面链接(最好是后者),会更容易回答您的问题。此外,正如 @j08691 所提到的,请务必仔细检查 WordPress Codex。他们在记录 CMS 的每个功能方面做得非常出色。

标签: html wordpress


【解决方案1】:

缺少博客名称。这就是为什么只显示页面标题的原因。尝试这样的事情有一个想法:

<title>
    <?php
    bloginfo( 'name' );
    echo " | ";
    if ( is_home() ) {
      _e( "Home" );
    }
    else {
      // Get the custom Title for this page, if any.
      if ( defined( 'Title' ) ) {
        $LimitWords = Title;
        echo string_limit_words( $LimitWords, 4 ) . " ...";
      }
      else {
      // Get the default Title
        $LimitWords = wp_title( '', FALSE );
        echo string_limit_words( $LimitWords, 4 ) . " ...";
      }
    }
    ?>
  </title>

要使该代码正常工作,您必须在样式表目录中的functions.php 中添加此函数:

  if ( !function_exists( 'string_limit_words' ) ) {
    function string_limit_words( $string, $word_limit ) {
      $words = explode( ' ', $string, ( $word_limit + 1 ) );
      if ( count( $words ) > $word_limit ) array_pop( $words );
      return implode( ' ', $words );
    }
  }

【讨论】:

  • 博客“名称”丢失是什么意思?切换到另一个主题时,会出现博客名称。那么这不意味着博客名称已设置吗?
  • &lt;title&gt;&lt;?php wp_title( '|', true, 'right' ); ?&gt;&lt;/title&gt; 这就是你的问题。那里,博客名称不见了。
  • 但这是 2012 主题的确切代码。它在 2012 主题中运行良好。我明白你在说什么,但现在我只是对 2012 主题如何与该代码一起使用感到困惑,而我的主题不适用于它。
  • 我也不能说。并非没有看到它,而是尝试我的答案中的代码,以便更好地了解需要什么。
  • Hrm... 我刚刚开始工作。我按 ctrl + f'ed 我的方式在 2012 年主题中找到一个处理标题的函数。我正在更新我的帖子以显示我添加的内容。注意:现在效果很好。抱歉,浪费了您的时间。
猜你喜欢
  • 1970-01-01
  • 2012-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-15
  • 1970-01-01
相关资源
最近更新 更多