問題描述

在我的小孩主題 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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。