如何更改档案标题以使用分类标签的复数形式而不是单数形式

How to change the archive title to use the plural version of the taxonomy label instead of the singular

我有一个简单的 Wordpress 博客,有两个类别:slug 是 'comment' 和 'press_release'。当我在任一类别的 post 的 single.php 页面上时,它应该显示 'Comment' 或 'Press Release'。当我在任一类别的存档的 archive.php 上时,我想将其命名为 'Comments' 或 'Press Releases'。

我已将这段代码添加到 functions.php,但它只会让整个网站在浏览器中变成空白。

--- 更新代码 ---

来自 functions.php:

$labels = array(
    'name'              => _x( 'Kommentare', 'taxonomy general name'),
    'singular_name'     => _x( 'Kommentar', 'taxonomy singular name'),
    'menu_name'         => __( 'Kommentare')
);

$labels = array(
    'name'              => _x( 'Pressemitteilungen', 'taxonomy general name'),
    'singular_name'     => _x( 'Pressemitteilung', 'taxonomy singular name'),
    'menu_name'         => __( 'Pressemitteilungen')
);

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

    if( is_tax() ) {
        $tax = get_taxonomy( get_queried_object()->taxonomy );
        /* use the "name" label instead of "singular_name" to showl the plural version */
        $title = sprintf( __( '%1$s: %2$s' ), 
                 $tax->labels->name, 
                 single_term_title( '', false ) );
    }
    return $title;
});

索引(对于复数版本)如 archive.php:

<?php if (have_posts()) : ?>

   <?php $post = $posts[0]; ?>

      <?php if (is_category()) { ?>

         <h2><?php echo get_the_archive_title(); ?></h2>

      <?php } elseif (is_author()) { ?>

         <h2><?php _e('Veröffentlicht von %s'); ?></h2>

   <?php } ?>

(但回显 'Kategorie: Pressemitteilung' 而不是 'Pressemitteilungen'。)

在单个 post 页面上,例如 single.php 在通常的代码块中:

<?php foreach((get_the_category()) as $category) { echo $category->cat_name; } ?>

--- 结束更新的代码 ---

--- 新更新的代码 ---

add_action( 'init', 'create_taxonomies', 0 );
function create_taxonomies() {
    $labels = array(
        'name'          => _x( 'Pressemitteilungen', 'taxonomy general name'),
        'singular_name' => _x( 'Pressemitteilung', 'taxonomy singular name'),
        'menu_name'     => __( 'Pressemitteilungen')
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'pressemitteilung' )
    );

    register_taxonomy( 'pressemitteilung', array( 'post' ), $args );

        $labels = array(
            'name'              => _x( 'Kommentare', 'taxonomy general name'),
            'singular_name'     => _x( 'Kommentar', 'taxonomy singular name'),
            'menu_name'         => __( 'Kommentare')
        );

        $args = array(
            'hierarchical'      => true,
            'labels'            => $labels,
            'query_var'         => true,
            'rewrite'           => array( 'slug' => 'kommentar' )
        );

        register_taxonomy( 'kommentar', array( 'post' ), $args );
}

add_filter( 'get_the_archive_title', function ( $title ) {
    if( is_tax() ) {
        $tax = get_taxonomy( get_queried_object()->taxonomy );
        $title = sprintf( __( '%2$s' ),
                 $tax->labels->name,
                 single_term_title( '', false ) );
    }
    return $title;
});

--- 结束新更新的代码 ---

请注意,我无法通过在“%s”之后添加字母 's' 来 "soft-fix" 它。我不能使用它,因为我想在德语博客上使用它,而且那里的复数形式并不完全一致(比如带有额外的 's)。

是语法错误还是我完全错了?

WP 默认对分类档案使用单一标签。

archive.php 使用 get_the_archive_title 来显示标题,它使用 singular_name 正如我们从 get_the_archive_title 的这行代码中看到的那样,它生成了分类法的标题档案:

 $title = sprintf( __( '%1$s: %2$s' ), 
         $tax->labels->singular_name, 
         single_term_title( '', false ) );

幸运的是,我们可以使用 get_the_archive_title 过滤器对其进行过滤。在我们的过滤器功能中,我们需要 1.检查档案是否用于分类 2. 如果是,从查询中获取分类 object 3. 与get_the_archive_title函数一样生成标题,但是使用'name'代替'singular_name'.

将此添加到您的 functions.php 以更改分类存档标题:

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

    if( is_tax() ) {
        $tax = get_taxonomy( get_queried_object()->taxonomy );
        /* use the "name" label instead of "singular_name" to showl the plural version */
        $title = sprintf( __( '%1$s: %2$s' ), 
                 $tax->labels->name, 
                 single_term_title( '', false ) );
    }
    return $title;
});

示例:

在您的问题中,您将标签定义如下:

$labels = array(
    'name'              => _x( 'Comments', 'taxonomy general name'),
    'singular_name'     => _x( 'Comment', 'taxonomy singular name'),
    'menu_name'         => __( 'Comments')
);

默认情况下,WP 将在存档中使用 singular_name,即 "Comment"。上面的代码将其改为使用 name,即 "Comments".

将此更改为使用您的德语标签:

$labels = array(
    'name'              => _x( 'Kommentare', 'taxonomy general name'),
    'singular_name'     => _x( 'Kommentar', 'taxonomy singular name'),
    'menu_name'         => __( 'Kommentare')
);

上面的代码现在将使用设置为 name 的 'Kommentare',而不是 singular_name 中的 "Kommentar"。

更新:

下面是我添加到测试站点的完整代码,它有效:

functions.php:

add_action( 'init', 'create_taxonomies', 0 );
function create_taxonomies() { 
    $labels = array(
        'name'          => _x( 'Pressemitteilungen', 'taxonomy general name'),
        'singular_name' => _x( 'Pressemitteilung', 'taxonomy singular name'),
        'menu_name'     => __( 'Pressemitteilungen')
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'press_release' )
    );

    register_taxonomy( 'press_release', array( 'post' ), $args );    
}

add_filter( 'get_the_archive_title', function ( $title ) {
    if( is_tax() ) {
        $tax = get_taxonomy( get_queried_object()->taxonomy );
        $title = sprintf( __( '%1$s: %2$s' ), 
                 $tax->labels->name, 
                 single_term_title( '', false ) );
    }
    return $title;
});

archive.php

<?php if ( have_posts() ) : ?>
    <h1 class="page-title"><?php echo get_the_archive_title( ); ?></h1>
    [...]

我在 WP 管理员的 press_release 分类法中设置了一个测试术语 testterm1,结果 HTML 在我的网站的存档页面中。com/press_release/testterm1 /

<h1 class="page-title">Pressemitteilungen: test term1</h1>