【问题标题】:Filter implode title and separator string wordpress过滤内爆标题和分隔符字符串 wordpress
【发布时间】:2017-03-03 21:35:25
【问题描述】:

我想替换这个字符串的一部分:

$title = implode( " $sep ", array_filter( $title ) );

这里的完整功能:

function wp_get_document_title() {
$title = apply_filters( 'pre_get_document_title', '' );
if ( ! empty( $title ) ) {
    return $title;
}

global $page, $paged;

$title = array(
    'title' => '',
);

if ( is_404() ) {
    $title['title'] = __( 'Page not found' );
} elseif ( is_search() ) {
    $title['title'] = sprintf( __( 'Search Results for “%s”' ), get_search_query() );
} elseif ( is_front_page() ) {
    $title['title'] = get_bloginfo( 'name', 'display' );
} elseif ( is_post_type_archive() ) {
    $title['title'] = post_type_archive_title( '', false );
} elseif ( is_tax() ) {
    $title['title'] = single_term_title( '', false );
} elseif ( is_home() || is_singular() ) {
    $title['title'] = single_post_title( '', false );
} elseif ( is_category() || is_tag() ) {
    $title['title'] = single_term_title( '', false );
} elseif ( is_author() && $author = get_queried_object() ) {
    $title['title'] = $author->display_name;
} elseif ( is_year() ) {
    $title['title'] = get_the_date( _x( 'Y', 'yearly archives date format' ) );
} elseif ( is_month() ) {
    $title['title'] = get_the_date( _x( 'F Y', 'monthly archives date format' ) );

} elseif ( is_day() ) {
    $title['title'] = get_the_date();
}

if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
    $title['page'] = sprintf( __( 'Page %s' ), max( $paged, $page ) );
}

if ( is_front_page() ) {
    $title['tagline'] = get_bloginfo( 'description', 'display' );
} else {
    $title['site'] = get_bloginfo( 'name', 'display' );
}

$sep = apply_filters( 'document_title_separator', '-' );

$title = apply_filters( 'document_title_parts', $title );

$title = implode( " $sep ", array_filter( $title ) );
$title = wptexturize( $title );
$title = convert_chars( $title );
$title = esc_html( $title );
$title = capital_P_dangit( $title );

return $title;
}

function _wp_render_title_tag() {
    if ( ! current_theme_supports( 'title-tag' ) ) {
        return;
    }
    echo '<title>' . wp_get_document_title() . '</title>' . "\n";
}

但是,这是在wp-includes/general-template.php 下,我想通过我的functions.php 过滤它,以免影响核心文件。

有没有办法过滤掉$sep 函数前后的空格?本质上,我希望标题因 SEO 原因而更改,并且只希望分隔符出现在首页上.. 沿着这些线的东西(这当然不起作用):

<?php if ( is_front_page() ) { 
    $title = implode( "", array_filter( $title ) ); 
} else { 
    $title = implode( " $sep ", array_filter( $title ) ); 
} ?>

有没有办法过滤或使用str_replace

【问题讨论】:

  • $sep 在其中未定义,因此无论您尝试使用它都行不通。
  • 可能有一个答案(这有点像 hack),但是为了给你一个好的答案,这个问题缺少一个关键部分:这在哪里被调用?调用后你可以访问完整的字符串吗?
  • @cale_b 谢谢,我在那里添加了完整的功能
  • 在上面的 is_front_page()... 代码中,您传入了 $sep - 它在哪里定义?
  • @cale_b $sep 在我要修改的字符串上方几行的function wp_get_document_title() 中定义

标签: php string wordpress filter


【解决方案1】:

在 WordPress 4.4+ 中尝试document_title_separator 过滤器和pre_get_document_title

将分隔符设置为$%,然后将其与preg_replace 结合使用。然后,您可以将模式与添加的空格(例如 '/ \$% /')与整个标题进行匹配。

functions.php:

<?php

if ( is_front_page() ) {
    add_filter('pre_get_document_title', 'theme_mod_title');
}

function theme_mod_title() {

    add_filter('document_title_separator', function() {
        return '$%';
    });

    remove_filter('pre_get_document_title', 'theme_mod_title');

    $pattern = '/ \$% /';
    $desired = '-';
    $title = wp_get_document_title();

    return preg_replace($pattern, $desired, $title);
}

【讨论】:

  • 不会这样做,因为 $title = implode( " $sep ", array_filter( $title ) );出现在 document_title_separator 之后
  • @Kevin 我反对修改 general-template.php 的表单,因为它是一个核心文件,这就是我想通过 functions.php 过滤的原因
  • 感谢凯文,与if ( is_front_page() ) { } 通话完美结合
猜你喜欢
  • 1970-01-01
  • 2019-07-04
  • 1970-01-01
  • 1970-01-01
  • 2017-01-14
  • 2013-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多