카테고리에 상위 카테고리가 있는지 확인하는 방법
categoryone에 부모가 있는지 확인하려고 합니다.카테고리 1이라는 카테고리가 있는지 확인할 수 있지만 카테고리 1에 상위 카테고리가 있는 경우에는 확인할 수 없습니다.나는 아래의 코드와 같은 것을 코드화하려고 했다.
$tid = term_exists('categoryone', 'category', 0);
$term_ids = [];
if ( $tid !== 0 && $tid !== null )
{
$term_ids[] = $tid['term_id'];
}
else
{
// If there is not a parent category!
$insert_term_id = wp_insert_term( 'categoryone', 'category' );
if ( ! is_wp_error )
$term_ids[] = $insert_term_id;
}
wp_set_post_categories( $insert_id, $term_ids );
다음과 같은 것을 사용할 수 있습니다(이것을 에 붙여넣습니다).functions.php파일)
function category_has_parent($catid){
$category = get_category($catid);
if ($category->category_parent > 0){
return true;
}
return false;
}
템플릿에서 호출
if(category_has_parent($tid)) {
// it has a parent
}
Children 체크
function has_Children($cat_id)
{
$children = get_terms(
'category',
array( 'parent' => $cat_id, 'hide_empty' => false )
);
if ($children){
return true;
}
return false
}
템플릿에서 호출
if(has_Children($tid)) {
// it has children
}
get_category()를 사용하여 term_id를 전달하여 현재 카테고리의 세부 정보를 가져올 수 있습니다.
$tid = term_exists('categoryone', 'category', 0);
$t_details=get_category($tid);
if(!empty($t_details->parent)){
echo $t_details->parent; // parent category id or $t_details->category_parent
}
get_category()는 참조 사이트에서 가져온 다음과 같은 개체를 반환합니다.
stdClass Object
(
[term_id] => 85
[name] => Category Name
[slug] => category-name
[term_group] => 0
[term_taxonomy_id] => 85
[taxonomy] => category
[description] =>
[parent] => 70
[count] => 0
[cat_ID] => 85
[category_count] => 0
[category_description] =>
[cat_name] => Category Name
[category_nicename] => category-name
[category_parent] => 70
)
EDIT 자식 카테고리를 가져오려면 인수 배열을 전달하여 get_categories()를 사용합니다.
부모
(정수) ID로 식별되는 카테고리의 직계 하위(자녀만) 카테고리만 표시합니다.
$args=array('parent'=>$tid);
$child_categories=get_categories($args);
를 사용할 수 있습니다(링크를 보고 인수를 읽는 등).그러면 계층 순서대로 모든 부모 목록이 반환됩니다.이 경우 가장 직접적인 부모 카테고리만 원하는 경우 다음과 같이 수행할 수 있습니다.
<?php
$parent_cats = get_category_parents( $cat, false, ',' );
$parent_cat = explode(",", $parent_cats);
echo $parent_cat[0];
?>
그러면 첫 번째 부모 카테고리(현재 카테고리 바로 위에 있는 카테고리)가 에코아웃됩니다.
명확히 하자면:
에get_category_parents()
- arg 1은 카테고리 ID입니다(사용했을 뿐입니다).$cat임의로)
- arg 2는 wp가 반환된 각 부모 카테고리에 링크를 추가하고 싶은 경우입니다.
arg 3은 반환된 카테고리가 있는 경우 구분하기 위해 사용되는 구분자입니다.
에explode()
arg 1은 배열에서 문자열을 구분하기 위해 찾는 구분자입니다.
- arg 2는 분리하는 소스 문자열입니다.
해피 코딩!
카테고리의 상위 및 하위 항목을 확인하려면 아래 코드를 사용하십시오.
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); // get current term
$parent = get_term($term->parent, get_query_var('taxonomy') ); // get parent term
$children = get_term_children($term->term_id, get_query_var('taxonomy')); // get children
if(($parent->term_id!="" && sizeof($children)>0)) {
// has parent and child
}elseif(($parent->term_id!="") && (sizeof($children)==0)) {
// has parent, no child
}elseif(($parent->term_id=="") && (sizeof($children)>0)) {
// no parent, has child
}
@ M 칼리드 주나이드
당신의 답변을 확대하기 위해, 저는 당신의 코드의 근거를 사용하여 그 카테고리에 부모가 없고 카테고리/택시의 상위 항목인지 확인했습니다.제가 이걸 포함시키는 이유는 제가 이 실타래를 우연히 발견했을 때 찾고 있던 것이고, 어쩌면 다른 사람도 같은 일을 하고 있을 수도 있기 때문입니다.
<?php
$args = array(
'taxonomy' = 'categories'; // If using custom post types, type in the custom post type's name
);
$terms = get_terms( $args );
foreach ( $terms as $term ) {
$has_parent = $term->parent;
$name = $term->name;
// If it doesn't have parents...
if ( !$has_parent ) {
// ...then it's the top tier in a hierarchy!
echo "Tier 1:" $name . '<br />';
}
}
?>
산출량
계층 1: 카테고리 이름
계층 1: 다른 카테고리 이름
계층 1: 모든 최상위 카테고리
$queried = $wp_query->get_queried_object();
if ($queried->category_parent) {
// category has parent
}
먼저 분류법 'taxonomy_name'에서 모든 용어를 가져온 다음 각각에 부모가 있는지 확인합니다.아래 코드를 사용해 보십시오.
<?php
$service_terms= get_terms( array(
'taxonomy' => 'taxonomy_name'
) );
?>
<?php if(!empty($service_terms)) : ?>
<?php foreach($service_terms as $term) : ?>
<?php $has_parent = $term->parent; ?>
<?php if (!$has_parent): ?>
<!-- If this term is a parent then put your code here -->
<?php endif ?>
<?php endforeach ?>
<?php endif ?>
언급URL : https://stackoverflow.com/questions/19064875/how-to-check-if-a-category-has-a-parent-category
'programing' 카테고리의 다른 글
| 봄 MVC 뷰 레이어용 JSP 대체 방법 (0) | 2023.03.21 |
|---|---|
| 환경변수를 사용한 스프링 부트 구성 (0) | 2023.03.21 |
| Angular.js 지시 동적 템플릿URL (0) | 2023.03.21 |
| @Autowired 컨스트럭터 파라미터를 개별적으로 "required=false"로 설정하는 방법 (0) | 2023.03.21 |
| Rspec 테스트를 사용하여 rails json API 예제를 완료합니다. (0) | 2023.03.21 |