我想在网站上显示所选分类的父级。为此,我有上面的代码,但问题在于它在具有现在结构的列表中显示所有选定的分类法。
global $post;
$features = get_the_terms( $post->ID, 'property-feature' );
if ( !empty( $features ) && is_array( $features ) && !is_wp_error( $features ) ) {
?>
<div class="property-features">
<?php
global $inspiry_options;
$property_features_title = $inspiry_options[ 'inspiry_property_features_title' ];
if( !empty( $property_features_title ) ) {
?><h4 class="fancy-title"><?php echo esc_html( $property_features_title ); ?></h4><?php
}
?>
<ul class="property-features-list clearfix">
<?php
foreach( $parent_term as $single_feature ) {
echo '<li><a href="' . get_term_link( $single_feature->slug, 'property-feature' ) .'">'. $single_feature->name . '</a></li>';
}
?>
</ul>
</div>
<?php
例子:
父级:选项1,选项2
父级 2:选项 3,选项 4
如果我选择选项2和选项3我想看
父级:选项 2
父级 2:选项 3
更新 2
global $post;
$features = get_the_terms( $post->ID, 'property-feature' );
$featuresss = get_the_terms( $post->ID, 'property-feature' );
// determine the topmost parent of a term
function get_term_top_most_parent($term_id, $taxonomy){
// start from the current term
$parent = get_term_by( 'id', $term_id, $taxonomy);
// climb up the hierarchy until we reach a term with parent = '0'
while ($parent->parent != '0'){
$term_id = $parent->parent;
$parent = get_term_by( 'id', $term_id, $taxonomy);
}
return $parent;
}
// so once you have this function you can just loop over the results returned by wp_get_object_terms
function hey_top_parents($taxonomy, $results = 1) {
// get terms for current post
$terms = wp_get_object_terms( get_the_ID(), $taxonomy );
$y = get_the_terms($terms->term_id, $taxonomy);
// set vars
$top_parent_terms = array();
foreach ( $terms as $term ) {
//get top level parent
$top_parent = get_term_top_most_parent( $term->term_id, $taxonomy );
//check if you have it in your array to only add it once
if ( !in_array( $top_parent, $top_parent_terms ) ) {
$top_parent_terms[] = $top_parent;
}
}
// build output (the HTML is up to you)
foreach( $top_parent_terms as $single_feature ) {
echo '<li><a href="' . get_term_link( $single_feature->slug, 'property-feature' ) .'">'. $single_feature->name . '</a></li>';
foreach( $terms as $single ) {
echo '<ul><li><a href="' . get_term_link( $single->slug, 'property-feature' ) .'">'. $single->name . '</a></li></ul>';
}
}
//return $top_parent_terms;
}
我设法显示所选分类的顶级父级,但现在的问题是我现在需要在顶级父级中仅显示来自该父级的所选分类
最佳答案
global $post;
$taxonomy = "property-feature";
// determine the topmost parent of a term
function get_term_top_most_parent($term_id, $taxonomy){
// start from the current term
$parent = get_term_by( 'id', $term_id, $taxonomy);
// climb up the hierarchy until we reach a term with parent = '0'
while ($parent->parent != '0'){
$term_id = $parent->parent;
$parent = get_term_by( 'id', $term_id, $taxonomy);
}
return $parent;
}
// so once you have this function you can just loop over the results returned by wp_get_object_terms
function hey_top_parents($taxonomy, $results = 1) {
// get terms for current post
$terms = wp_get_object_terms( get_the_ID(), $taxonomy );
// set vars
$top_parent_terms = array();
foreach ( $terms as $term ) {
//get top level parent
$top_parent = get_term_top_most_parent( $term->term_id, $taxonomy );
//check if you have it in your array to only add it once
if ( !in_array( $top_parent, $top_parent_terms ) ) {
$top_parent_terms[] = $top_parent;
}
}
// build output (the HTML is up to you)
foreach( $top_parent_terms as $single_feature )
{
echo '<li><a href="' . get_term_link( $single_feature->slug, $taxonomy ) .'">'. $single_feature->name . '</a></li>';
foreach( $terms as $single ) {
if( $single_feature->term_id == $single->parent ) {
echo '<ul><li><a href="' . get_term_link( $single->slug, $taxonomy ) .'">'. $single->name . '</a></li></ul>';
}
}
}
//return $top_parent_terms;
}
变化:
1) 将分类法名称移动到变量(在 echo 中使用)。
$taxonomy = "property-feature";
2) 在负责显示子条款的代码中添加了 if 条件。
if( $single_feature->term_id == $single->parent ) {
echo '<ul><li><a href="' . get_term_link( $single->slug, $taxonomy ) .'">'. $single->name . '</a></li></ul>';
}
已删除(未使用的代码行):
1)
$features = get_the_terms( $post->ID, 'property-feature' );
$featuresss = get_the_terms( $post->ID, 'property-feature' );
2)
$y = get_the_terms($terms->term_id, $taxonomy);
考虑:
1) 从以下位置删除 $results 变量:
function hey_top_parents($taxonomy, $results = 1) {
它没有在任何地方使用,或者可能应该用来确定函数是否应该返回或显示结果。
关于php - 如何显示所选分类的分类父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38511639/