맞춤 분류 또는 카테고리에서 상위 용어 만 가져 오는 방법이 있습니까?맞춤 분류 또는 카테고리에서 상위 용어 만 가져 오는 방법이 있습니까?
$genres = get_the_term_list($post->ID, 'genres', '<div class="genres"><div class="mta">', '', '</div></div>');
맞춤 분류 또는 카테고리에서 상위 용어 만 가져 오는 방법이 있습니까?맞춤 분류 또는 카테고리에서 상위 용어 만 가져 오는 방법이 있습니까?
$genres = get_the_term_list($post->ID, 'genres', '<div class="genres"><div class="mta">', '', '</div></div>');
어린이 없이도 목록을 만들 수 있습니다.
/**
* @param post_id can use 'get_the_ID()'
* @param taxonomy for example 'category'
*/
$terms = wp_get_object_terms($post_id, $taxonomy, array('parent'=>'0'));
foreach($terms as $term) {
?>
<a href="<?php echo get_term_link($term->term_id); ?>"><?php echo $term->name; ?></a>
<?php
}
두 번째 옵션
는 조건을 필터링하고get_the_term_list()
기능 0보다 큰 부모와 모든 조건을 제거 할 수 있다는 것입니다.
function remove_child_terms($terms, $post_id, $taxonomy) {
/**
* Add some condition here to limit this for your custom taxonomy
* if($taxonomy == 'something') {
*/
foreach($terms as $key => $term) {
if($term->parent !== 0) {
unset($terms[$key]);
}
}
/**
* }
*/
return $terms;
}
add_filter('get_the_terms', 'remove_child_terms', 10, 3);
하는 경우 '$ taxonomy'를 택 소노 미와 대체 할 필요가있는 첫 번째 코드를 사용합니다. 두 번째 코드를 사용한다면 functions.php 안에 넣어야합니다. – Shibi
이 코드를 시도
<?php $terms = get_terms(array(
'taxonomy' => 'taxonomy_name',
'parent' => 0
));
if (! empty($terms) && ! is_wp_error($terms)){
echo '<ul>';
foreach ($terms as $term) {
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
} ?>
'parent' 매개 변수를'0'으로 전달하면 부모없는 계층 적 택 소노 미. – plushyObject
보시다시피 그는'$ post-> ID'를 전달하고'get_the_term_list()'를 사용하여 게시물과 관련된 용어를 원합니다 ... – Shibi
이 답변을 참조하시기 바랍니다 https://wordpress.stackexchange.com/questions/24582/how-can-i-get-only-parent-terms –