根据本帖中的讨论,我设置了一个函数并开始工作:Custom taxonomy, get_the_terms, listing in order of parent > child.
我的版本包括术语链接,并允许我在单个中显示术语信息。php以一种看起来像面包屑的方式。然而,我设置了一个自定义术语,我永远不想向用户显示,因为它是一个用于内容编辑器的挂钩,以便他们可以在站点其他区域的特定循环顶部显示帖子。
以前,我有一个exclude get\\u the\\u term\\u list函数,但它没有按顺序显示。
如何修复函数,使术语按顺序显示,但X术语不显示。
我当前的功能(排除当前工作不正常)如下所示:
function terms_by_order( $terms, $taxonomy, $exclude = array() ) {
// check input
if ( empty( $terms ) || is_wp_error( $terms ) || ! is_array( $terms ) )
return;
// set id variables to 0 for easy check
$grandparent_id = $parent_id = $term_id = 0;
// get grandparent
foreach ( $terms as $term ) {
if ( $grandparent_id || $term->parent && !($exclude) )
continue;
$grandparent_id = $term->term_id;
$grandparent_slug = $term->slug;
$grandparent_url = \'<a href="\'.get_term_link($grandparent_slug, $taxonomy).\'">\'.$term->name.\'</a>\';
}
// get parent
foreach ( $terms as $term ) {
if ( $parent_id || $grandparent_id != $term->parent && !($exclude) )
continue;
$parent_id = $term->term_id;
$parent_slug = $term->slug;
$parent_url = \'<a href="\'.get_term_link($parent_slug, $taxonomy).\'">\'.$term->name.\'</a>\';
}
// get child
foreach ( $terms as $term ) {
if ( $parent_id || $parent_id != $term->parent && !($exclude) )
continue;
$term_id = $term->term_id;
$term_slug = $term->slug;
$term_url = \'<a href="\'.get_term_link($term_slug, $taxonomy).\'">\'.$term->name.\'</a>\';
}
echo "$grandparent_url / $parent_url / $term_url";
}
这是我在单曲上使用的标签。php将此调用为操作
terms_by_order( get_the_terms( $post->ID, \'news_category\' ),\'news_category\',array(3623) );
正确执行时的外观示例如下:
音乐/采访/
最合适的回答,由SO网友:Ashkas 整理而成
唉,没有答案或评论!:P
不用担心,一位同事在这里帮助了我,下面是上面使用$exclude的代码
function terms_by_order($taxonomy, $exclude) {
global $post;
$terms = get_the_terms($post->ID, $taxonomy);
// check input
if ( empty($terms) || is_wp_error($terms) || !is_array($terms) ) return;
// exclude
foreach ($terms as $key=>$term) {
if (in_array($key, $exclude)) { // key in term_array is also term_id..
unset($terms[$key]);
break;
}
}
foreach ($terms as $key=>$term) {
$parent_term = $term; // gets last parent (should we get only the first one?)
if ($term->parent != 0) { // if there is a child, find it
$child_term = $term; // get the child term...
$parent_term = get_term_by(\'id\', $term->parent, $taxonomy); // ... and the parent term
break;
}
}
if (isset($parent_term)) echo \'<a href="\'.get_term_link($parent_term, $taxonomy).\'">\'.$parent_term->name.\'</a>\';
if (isset($child_term)) echo \' / <a href="\'.get_term_link($child_term, $taxonomy).\'">\'.$child_term->name.\'</a>\';
}