更改类别显示名称功能

时间:2021-06-02 作者:markw

有没有更聪明的方法来编写此函数?可能是通配符?

add_filter( \'get_the_archive_title\', function ( $title ) {
    if ( is_category( \'alphabetical\' ) ) {
        $title = \'All Characters\';
    }
    elseif ( is_category( \'a-alphabetical\' ) ) {
        $title = \'Characters Starting with A\';
    }
    elseif ( is_category( \'b-alphabetical\' ) ) {
        $title = \'Characters Starting with B\';
    }
    elseif ( is_category( \'c-alphabetical\' ) ) {
        $title = \'Characters Starting with C\';
    }
    elseif ( is_category( \'d-alphabetical\' ) ) {
        $title = \'Characters Starting with D\';
    }
    elseif ( is_category( \'e-alphabetical\' ) ) {
        $title = \'Characters Starting with E\';
    }
    elseif ( is_category( \'f-alphabetical\' ) ) {
        $title = \'Characters Starting with F\';
    }
    elseif ( is_category( \'g-alphabetical\' ) ) {
        $title = \'Characters Starting with G\';
    }
    elseif ( is_category( \'h-alphabetical\' ) ) {
        $title = \'Characters Starting with H\';
    }
    elseif ( is_category( \'i-alphabetical\' ) ) {
        $title = \'Characters Starting with I\';
    }
    elseif ( is_category( \'j-alphabetical\' ) ) {
        $title = \'Characters Starting with J\';
    }
    elseif ( is_category( \'k-alphabetical\' ) ) {
        $title = \'Characters Starting with K\';
    }
    elseif ( is_category( \'l-alphabetical\' ) ) {
        $title = \'Characters Starting with L\';
    }
    elseif ( is_category( \'m-alphabetical\' ) ) {
        $title = \'Characters Starting with M\';
    }
    elseif ( is_category( \'n-alphabetical\' ) ) {
        $title = \'Characters Starting with N\';
    }
    elseif ( is_category( \'o-alphabetical\' ) ) {
        $title = \'Characters Starting with O\';
    }
    elseif ( is_category( \'p-alphabetical\' ) ) {
        $title = \'Characters Starting with P\';
    }
    elseif ( is_category( \'q-alphabetical\' ) ) {
        $title = \'Characters Starting with Q\';
    }
    elseif ( is_category( \'r-alphabetical\' ) ) {
        $title = \'Characters Starting with R\';
    }
    elseif ( is_category( \'s-alphabetical\' ) ) {
        $title = \'Characters Starting with S\';
    }
    elseif ( is_category( \'t-alphabetical\' ) ) {
        $title = \'Characters Starting with T\';
    }
    elseif ( is_category( \'u-alphabetical\' ) ) {
        $title = \'Characters Starting with U\';
    }
    elseif ( is_category( \'v-alphabetical\' ) ) {
        $title = \'Characters Starting with V\';
    }
    elseif ( is_category( \'w-alphabetical\' ) ) {
        $title = \'Characters Starting with W\';
    }
    elseif ( is_category( \'x-alphabetical\' ) ) {
        $title = \'Characters Starting with X\';
    }
    elseif ( is_category( \'y-alphabetical\' ) ) {
        $title = \'Characters Starting with Y\';
    }
    elseif ( is_category( \'z-alphabetical\' ) ) {
        $title = \'Characters Starting with Z\';
    }
    return $title;
});

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

所以我想知道为什么你不直接通过管理页面设置类别标题,例如,对于有slug的那一个alphabetical, 你可以设置All Characters 作为类别的静态标题?

那样的话,你就不需要get_the_archive_title..

但无论如何,这里有一种方法可以减少这些(26)elseif 变成一个:(使用正则表达式)

add_filter( \'get_the_archive_title\', function ( $title ) {
    if ( is_category( \'alphabetical\' ) ) {
        $title = \'All Characters\';
    // If the slug starts with <letter>-alphabetical, we set the title to
    // "Characters Starting with <the LETTER in uppercase>".
    } elseif ( is_category() && preg_match( \'/^([a-z])-alphabetical$/\', get_queried_object()->slug, $m ) ) {
        $title = \'Characters Starting with \' . strtoupper( $m[1] );
    }

    return $title;
} );
所以在上面的代码中,我使用preg_match(), 还有get_queried_object() 返回类别存档页上当前类别的术语对象/数据(例如example.com/category/alphabetical/).