在 Wordpress 中get_the_term_list的最后一个值之前使用'and'



有没有办法在get_the_term_list的输出中使用单词"and"?

<?php echo strip_tags(get_the_term_list( $post->ID, 'stijl', ' ',', ')); ?>

此代码产生以下输出:

Onverwachte wending,Pakkend,Slasher,Slim,Spanning

但我希望它是:

Onverwachte wending,Pakkend,Slasher,Slim and Spanning

所以基本上我希望最后一个逗号变成"and"。不知道如何做到这一点。这可能吗?

在WordPress中,get_the_term_list返回get_the_terms的格式化版本,其返回帖子上的术语的数组。我们可以使用它来自定义列表的格式。

通常,当我这样做时,我会在functions.php中添加以下内容:

// In functions.php
function so_63957175_niceTermList($taxonomy = 'category') {
global $post;
$term_list = '';
$terms = get_the_terms($post->ID, $taxonomy);
$n = 1;
if ($terms) {
foreach($terms as $term) {
if ($n < count($terms)) {
$term_list .= $term->name . ', ';
} else {
$term_list = rtrim($term_list, ', ') .  ' and ' . $term->name;
}
++$n;
}
}
$term_list = rtrim($term_list, ', ');
return $term_list;
}
// In your template file
<?php echo so_63957175_niceTermList('category'); ?>

相关内容

最新更新