반응형
둘 이상의 항목을 선택한 경우 기본 범주를 가져오시겠습니까?
Wordpress에서 기본 범주로 되돌리려면 어떻게 해야 합니까?
다음 루프를 사용하고 있는데, 이 세 가지를 모두 선택하면 마지막 항으로 돌아갑니다.그것이 주요 카테고리인지 확인하고 싶습니다.
<?php $term_list = wp_get_post_terms($post->ID, 'category', array("fields" => "names"));
foreach ($term_list as $term) {
$name = $term;
} ?>
이것은 원어민 워드프레스 기능이 아니라 Yoast SEO의 기능입니다(여기를 참조).
프라이머리 상태는 다음과 같이 확인할 수 있습니다.
<?php
$term_list = wp_get_post_terms($post->ID, 'category', ['fields' => 'all']);
foreach($term_list as $term) {
if( get_post_meta($post->ID, '_yoast_wpseo_primary_category',true) == $term->term_id ) {
// this is a primary category
}
}
?>
커스텀 분류법을 사용하는 경우 meta_key를 사용합니다.
_yoast_wpseo_primary_CUSTOM_TAXONOMY
대신.
"Yoast SEO" 대신 "The SEO Framework" 플러그인을 사용하는 경우:
$taxonomy = 'category';
$post_id = get_the_ID();
$terms = wp_get_post_terms($post_id, $taxonomy, ['fields' => 'all']);
$primary_term = intval(get_post_meta( $post_id, '_primary_term_' . $taxonomy, true ));
foreach($terms as $term) {
if( $primary_term == $term->term_id ) {
// this is a primary category
}
}
참고 자료:
Yoast 플러그인 인덱싱 및 최적화를 사용한 경우 승인된 답변이 작동하지 않습니다.
$wpseo_primary_term = new WPSEO_Primary_Term( 'category', get_the_id() );
$wpseo_primary_term = $wpseo_primary_term->get_primary_term();
$term = get_term( $wpseo_primary_term );
이게 올바른 방법일 거예요.
요스트가 비활성화되어도 사이트가 파손되지 않도록 하기 위해 코드를 로 감싸야 합니다.
if ( class_exists('WPSEO_Primary_Term') ) { }
플러그인 "Yoast SEO"를 사용하는 경우:
yoast_get_primary_term_id( 'product_cat', $post->id );
Rank Math SEO 플러그인을 사용하는 사용자는 다음을 사용할 수 있습니다.
get_post_meta( $post_id, 'rank_math_primary_category', true );
관련 투고 : https://wordpress.org/support/topic/get-primary-category-2/
이 함수를 사용하여 기본 범주를 가져올 수 있습니다.
<?php
function prefix_get_primary_category($post_id) {
if(class_exists('WPSEO_Primary_Term')) {
$wpseo_primary_term = new WPSEO_Primary_Term( 'category', $post_id );
$wpseo_primary_term = $wpseo_primary_term->get_primary_term();
$the_primary_term = get_term( $wpseo_primary_term );
if(!empty($the_primary_term) && !is_wp_error($the_primary_term)) {
return $the_primary_term;
}
}
return false;
}
언급URL : https://stackoverflow.com/questions/43114986/get-primary-category-if-more-than-one-is-selected
반응형
'prosource' 카테고리의 다른 글
Amazon EC2(Amazon Linux)에서 WordPress에 대한 사용 권한 설정 (0) | 2023.02.08 |
---|---|
3분마다 Word press cronjob 실행 (0) | 2023.02.08 |
하이차트에서 범위 및 네비게이터 기능을 사용하여 열 범위 차트를 만드는 방법은 무엇입니까? (0) | 2023.02.08 |
프런트 엔드의 OAuth를 사용한 인증에 성공한 후 백엔드와 대화하는 방법 (0) | 2023.02.08 |
SQL을 통해 Wordpress 비밀번호를 재설정하시겠습니까? (0) | 2023.02.08 |