[英]wordpress : how to add commas between meta terms on single custom post type
extendeing may last question about how to change the display of meta on single custom post type, with a great thanks to TimRDD for his helpful answer, now i have another question. 扩展可能最后一个问题,关于如何更改单个自定义帖子类型上的元显示,非常感谢TimRDD的有用回答,现在我还有另一个问题。 the working generating code
工作生成代码
<?php
//get all taxonomies and terms for this post (uses post's post_type)
foreach ( (array) get_object_taxonomies($post->post_type) as $taxonomy ) {
$object_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'all'));
if ($object_terms) {
echo '- '.$taxonomy;
foreach ($object_terms as $term) {
echo '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> ';
}
}
}
}
?>
displays the terms in a single row but without commas between them such as : (- Proceeding2015 - keywordsbusinessconsumerresearch). 在单行中显示字词,但在它们之间没有逗号,例如:(-Proceeding2015-keywordbusinessconsumerresearch)。
I need your help please to put (:) after every set of terms and commas between terms to display them such as : (- proceeding : 2015 - keywords : business, consumer, research). 我需要您的帮助,请在每套术语后加上(:),以显示它们,例如:(-进行中:2015-关键字:商业,消费者,研究)。
I've not tested this code, but I have linted it. 我尚未测试此代码,但已将其掉毛了。 Based on your description, this should do it.
根据您的描述,应该这样做。
<?php
//get all taxonomies and terms for this post (uses post's post_type)
I moved this out of the fornext
. 我搬到这个出的
fornext
。
$taxonomies = get_object_taxonomies($post->post_type);
foreach ( $taxonomies as $taxonomy ) {
I moved this into an if
statement. 我将其移至
if
语句中。 If the assignment fails (nothing is returned) it should fail the if
and skip all of this. 如果分配失败(不返回任何内容),则应该失败
if
并跳过所有这些。
if ($object_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'all'))) {
$holding = array();
foreach ($object_terms as $term) {
Instead of outputting it immediately, I am building an array. 我没有立即输出它,而是在构建一个数组。
$holding[] = '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> ';
} // foreach ($object_terms as $term)
Here is where we do that output. 这是我们进行输出的地方。 I'm using the
explode()
function. 我正在使用
explode()
函数。 This will output each element of the array and put a ', ' after all of them except for the last one. 这将输出数组的每个元素,并在所有元素之后添加一个“,”(最后一个元素除外)。
echo '- '.$taxonomy . ': ' .explode(', ',$holding) . ' ';
} // if ($object_terms)
} // foreach ( $taxonomies as $taxonomy )
?>
I do hope I got it right. 我希望我做对了。
Cheers! 干杯!
=C= = C =
You're code is ok, you just need to modify the output a bit. 您的代码还可以,您只需要稍微修改输出即可。 Try this code:
试试这个代码:
//get all taxonomies and terms for this post (uses post's post_type)
foreach ((array) get_object_taxonomies($post->post_type) as $taxonomy) {
$object_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'all'));
if ($object_terms) {
echo ': (- ' . $taxonomy . ': ';// I modify the output a bit.
$res = '';
foreach ($object_terms as $term) {
$res .= '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf(__("View all posts in %s"), $term->name) . '" ' . '>' . $term->name . '</a>, ';
}
echo rtrim($res,' ,').')';// I remove the last trailing comma and space and add a ')'
}
}
Hope it works. 希望它能工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.