Php 如何将归档标题更改为使用复数版本的分类法标签而不是单数版本
我有一个简单的Wordpress博客,分为两类:评论和新闻发布。当我在一个包含任意类别帖子的.php页面上时,它应该显示“评论”或“新闻稿”。当我在任一类别的archive.php上时,我想将其命名为“评论”或“新闻稿” 我已经将此代码添加到functions.php中,但它只会使整个网站在浏览器中变为空白 ---更新代码--- 从Php 如何将归档标题更改为使用复数版本的分类法标签而不是单数版本,php,wordpress,wordpress-theming,taxonomy,Php,Wordpress,Wordpress Theming,Taxonomy,我有一个简单的Wordpress博客,分为两类:评论和新闻发布。当我在一个包含任意类别帖子的.php页面上时,它应该显示“评论”或“新闻稿”。当我在任一类别的archive.php上时,我想将其命名为“评论”或“新闻稿” 我已经将此代码添加到functions.php中,但它只会使整个网站在浏览器中变为空白 ---更新代码--- 从functions.php: $labels = array( 'name' => _x( 'Kommentare', 'ta
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;
});
<?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 } ?>
在索引(复数版本)上,如archive.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;
});
<?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 } ?>
---结束更新代码---
---最新更新的代码---
---结束新更新的代码---
请注意,我无法通过在“%s”之后添加字母“s”来“软修复”它。我不能用这个词,因为我想在一个德国博客上用这个词,但复数形式并不完全一致(比如一个额外的)
这是语法错误还是我完全错了?默认情况下,WP使用单一标签进行分类归档 archive.php使用
get_the_archive_title
显示标题,标题使用单数名称,我们可以从get_the_archive_title
中的这行代码中看到,它生成分类法存档的标题:
$title = sprintf( __( '%1$s: %2$s' ),
$tax->labels->singular_name,
single_term_title( '', false ) );
幸运的是,我们可以使用get\u the\u archive\u title
过滤器对其进行过滤。在我们的过滤函数中,我们需要
1.检查归档文件是否用于分类
2.如果是,则从queries对象获取分类
3.生成标题的方法与获取\u存档\u标题
函数相同,但使用“名称”而不是“单数名称”
将其添加到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;
});
示例:
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将在存档中使用单数_名称
,即“注释”。上面的代码将其改为使用名称
,即“注释”
将此更改为使用德语标签:
$labels = array(
'name' => _x( 'Kommentare', 'taxonomy general name'),
'singular_name' => _x( 'Kommentar', 'taxonomy singular name'),
'menu_name' => __( 'Kommentare')
);
上面的代码现在将使用设置为名称的“Kommentare”,而不是单数名称中的“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>
[...]
这就是你的全部功能吗?您在结尾缺少一个}
作为开始。这个调用在哪里?换句话说,您在添加操作中使用了什么操作?另外,我对你上一个问题的回答有帮助吗,还是你需要更多的帮助?你从来都没有回应过……是的,很抱歉我还没能回来或者尝试一下。我会的!谢谢没问题,让我知道它是否有效或现在。不管怎样,这个问题。。。缺少的}
是您问题中的输入错误还是PHP中缺少?语法错误可能导致屏幕空白。如果是打字错误,您使用的是什么操作?我已经用您提到的两件事更新了这里的代码:-)/edit:它再次显示页面,但没有更改..当您说“无更改”时,您是指单数/复数吗?@ThomasMaier这意味着存档现在应该使用您为“名称”设置的值在$labels
数组中,而不是在“单数名称”中。我已经更新了答案,以展示其功能的示例。如果您已将$labels
设置为示例中英文标签的直接翻译,那么您应该在存档页面上获得“Kommentare”。@ThomasMaier使用single\u term\u title()
实际上是显式地告诉它使用单数术语!你能像这样包括存档标题吗
@ThomasMaier在$tax->$labels->name
中的前面标签中添加$
将不起作用-“label”是“$tax”对象中的一个变量,因此您必须像$tax->$labels
一样访问它。我遇到的问题是,我试图猜测您的代码的其余部分是什么。。。显然猜错了:)你能编辑你的问题,把你在单一主题页面和归档主题页面中使用的名称,以及你在标题中使用的代码都包括在内吗?@ThomasMaier我已经编辑了我的问题,将我添加的所有代码都包括在内,以便在我的测试站点上使用。请看一看,让我知道您自己的代码中有什么不同之处,这样我们就可以试着看看哪里出了问题。@ThomasMaier我不知道从哪里可以得到Kategorie
,因为您正在使用自定义分类法,就像在我的代码中一样,不是吗?因为您包含的用于设置标签的代码是用于设置分类的。如果你不这样做,那么你在哪里设置标签?