问题描述

在我的小孩主题 archive.php 中,我有以下代码显示我的档案页面的标题:

    <?php
        the_archive_title( '<h1 class="page-title">', '</h1>' );
    ?>

但是,它将我的标题显示为 “类别:类别标题”,而不是简单地标题,而没有预先添加的 “类别:” 。

我的第一个本能是从 wp-includes/general-template 中覆盖 get_the_archive_title()。但是从我所看到的,显然我不应该改变 wordpress 核心的东西,甚至从一个小孩主题的覆盖。

那么什么是 best-practice 方式来控制 the_archive_title()的输出?

最佳解决方案

如果您查看 get_the_archive_title()的源代码,您将看到提供了一个名为 get_the_archive_title 的过滤器,您可以通过该过滤器对函数的输出进行过滤。

您可以使用以下内容更改类别页面上的输出

add_filter( 'get_the_archive_title', function ( $title ) {

    if( is_category() ) {

        $title = single_cat_title( '', false );

    }

    return $title;

});

次佳解决方案

接受的答案是从类别归档标题中删除 Category: 前缀,但不能删除其他分类或帖子类型。要排除其他前缀,有两个选项:

  1. 重建原始 get_the_archive_title()功能中使用的所有变体的标题:

      

    // Return an alternate title, without prefix, for every type used in the get_the_archive_title().
    add_filter('get_the_archive_title', function ($title) {
        if ( is_category() ) {
            $title = single_cat_title( '', false );
        } elseif ( is_tag() ) {
            $title = single_tag_title( '', false );
        } elseif ( is_author() ) {
            $title = '<span class="vcard">' . get_the_author() . '</span>';
        } elseif ( is_year() ) {
            $title = get_the_date( _x( 'Y', 'yearly archives date format' ) );
        } elseif ( is_month() ) {
            $title = get_the_date( _x( 'F Y', 'monthly archives date format' ) );
        } elseif ( is_day() ) {
            $title = get_the_date( _x( 'F j, Y', 'daily archives date format' ) );
        } elseif ( is_tax( 'post_format' ) ) {
            if ( is_tax( 'post_format', 'post-format-aside' ) ) {
                $title = _x( 'Asides', 'post format archive title' );
            } elseif ( is_tax( 'post_format', 'post-format-gallery' ) ) {
                $title = _x( 'Galleries', 'post format archive title' );
            } elseif ( is_tax( 'post_format', 'post-format-image' ) ) {
                $title = _x( 'Images', 'post format archive title' );
            } elseif ( is_tax( 'post_format', 'post-format-video' ) ) {
                $title = _x( 'Videos', 'post format archive title' );
            } elseif ( is_tax( 'post_format', 'post-format-quote' ) ) {
                $title = _x( 'Quotes', 'post format archive title' );
            } elseif ( is_tax( 'post_format', 'post-format-link' ) ) {
                $title = _x( 'Links', 'post format archive title' );
            } elseif ( is_tax( 'post_format', 'post-format-status' ) ) {
                $title = _x( 'Statuses', 'post format archive title' );
            } elseif ( is_tax( 'post_format', 'post-format-audio' ) ) {
                $title = _x( 'Audio', 'post format archive title' );
            } elseif ( is_tax( 'post_format', 'post-format-chat' ) ) {
                $title = _x( 'Chats', 'post format archive title' );
            }
        } elseif ( is_post_type_archive() ) {
            $title = post_type_archive_title( '', false );
        } elseif ( is_tax() ) {
            $title = single_term_title( '', false );
        } else {
            $title = __( 'Archives' );
        }
        return $title;
    });
    
  2. 或者,简单地剥离看起来像标题前缀的任何东西 (可能会改变包含冒号字符的实际标题):

      

    // Simply remove anything that looks like an archive title prefix ("Archive:", "Foo:", "Bar:").
    add_filter('get_the_archive_title', function ($title) {
        return preg_replace('/^w+: /', '', $title);
    });
    

第三种解决方案

另一个选择是:

<?php echo str_replace('Brand: ','',get_the_archive_title()); ?>

替换品牌:您想要摆脱的任何文字。

值得研究的是 get_the_archive_title() 与 the_archive_title() the_archive_title() 之间的差异返回一个数组. get_the_archive_title() 返回一个字符串

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。