扩展可能是最后一个关于如何在单个自定义帖子类型上更改meta显示的问题,非常感谢TimRDD的有用回答,现在我有另一个问题。工作生成代码
<?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> ';
}
}
}
}
?>
在单行中显示术语,但字词之间没有逗号,例如:(- Proceedings 2015 - 关键词商业消费者研究)。
我需要你的帮助,请把(:)在每组术语和术语之间的逗号之后显示它们,例如:(- 会议记录 : 2015 - 关键词 : 商业, 消费者, 研究).
你的代码没问题,你只需要稍微修改一下输出。试试这个代码:
//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 ')'
}
}
希望它有效。
我没有测试过这段代码,但我已经检查了它。根据您的描述,这应该可以做到。
<?php
//get all taxonomies and terms for this post (uses post's post_type)
我把它从fornext
中移了出来.
$taxonomies = get_object_taxonomies($post->post_type);
foreach ( $taxonomies as $taxonomy ) {
我把它移到一个if
声明中。如果分配失败(不返回任何内容),则if
应失败并跳过所有这些操作。
if ($object_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'all'))) {
$holding = array();
foreach ($object_terms as $term) {
我不是立即输出它,而是构建一个数组。
$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)
这是我们进行输出的地方。我正在使用explode()
功能。这将输出数组的每个元素,并在除最后一个元素之外的所有元素之后放置一个 ', '。
echo '- '.$taxonomy . ': ' .explode(', ',$holding) . ' ';
} // if ($object_terms)
} // foreach ( $taxonomies as $taxonomy )
?>
我希望我做对了。
干杯!
=C=