【问题标题】:Php code in Wordpress title tagWordpress 标题标签中的 PHP 代码
【发布时间】:2018-06-13 13:35:29
【问题描述】:

我已经自定义了我的 php 标题标签来改变我的网站在主页和单个帖子中的外观。我的代码是:

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

它在单个帖子上就像一个魅力,但在我的主页上,我看不到“|”特点。如何解决?

【问题讨论】:

  • My code is: 你的代码在哪里?
  • @Rohil_PHPBeginner 如果不将代码指定为代码,浏览器会尝试将其解析为 html。在这种情况下,它尝试解析标题标签。
  • OP先写的时候,没有代码,他编辑后
  • 你必须echo它,因为你是insinde php tag

标签: php wordpress title


【解决方案1】:

试试

bloginfo('description') .'|'. bloginfo('name')

'|' character 表示 OR,如果你想将其用作字符,请使用引号。

【讨论】:

  • 我的 blogdesc 之间甚至没有空格。和 blogname,没有任何变化。
【解决方案2】:

'|'在 PHP 中表示“逻辑或”

试试看。 bloginfo('description'). '|' .bloginfo('name')

【讨论】:

  • 没有帮助,它没有改变。
  • 这对发布的其他答案有何改进?
  • @rnevius,我在还没有提交答案的时候写了答案!
【解决方案3】:

尝试在你的 function.php 中使用它

if ( ! function_exists( '_wp_render_title_tag' ) ) :
    function theme_slug_render_title() {
?>
<title><?php wp_title( '|', true, 'right' ); ?></title>
<?php
    }
    add_action( 'wp_head', 'theme_slug_render_title' );
endif;

【讨论】:

  • 一开始我试试这个代码&lt;title&gt;&lt;?php is_front_page() ? bloginfo('description')?&gt; | &lt;?php bloginfo('name') : wp_title(''); ? &gt;&lt;/title&gt;“|”会显示,但它会在单个帖子中复制我的博客名称,然后我更改为我面临这个问题的代码。
【解决方案4】:

根据WordPress Codex,主页有一点hack

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

对于内页

function theme_name_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', 'theme_name_wp_title', 10, 2 );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多