WordPress에서 Yoast seo 플러그인을 통해 기본 카테고리를 설정하는 방법
Yoast seo 플러그인을 사용하여 제품의 프라이머리 카테고리를 설정하였습니다.하지만 앞에서는 1차 카테고리명을 알 수 없습니다.
Yoast SEO 플러그인에 전용 기능이 추가되었습니다.yoast_get_primary_term_id()- 기본 용어의 ID를 가져옵니다.
$primary_term_id = yoast_get_primary_term_id( 'taxonomy_slug', $post_id_or_object );
그 후 를 사용할 수 있습니다.get_term()을 손에 넣다WP_Term오브젝트:
// Will return `false` if no primary term has been set
$primary_term_id = yoast_get_primary_term_id( 'taxonomy_slug', $post_id_or_object );
if ( $primary_term_id ) {
/** @var WP_Term $primary_term */
$primary_term = get_term( $primary_term_id );
}
용어 이름만 필요한 시나리오의 경우 다음을 사용할 수 있습니다.yoast_get_primary_term():
// Will return an empty string if no primary term has been set
$primary_term_name = yoast_get_primary_term( 'taxonomy_slug', $post_id_or_object );
안녕하세요 포스트메타 테이블을 이용하실 수 있습니다.
$taxonomy = 'product_cat';
$primary_cat_id=get_post_meta($product->id,'_yoast_wpseo_primary_' . $taxonomy, true);
if($primary_cat_id){
$primary_cat = get_term($primary_cat_id, $taxonomy);
if(isset($primary_cat->name))
echo $primary_cat->name;
}
나는 일에 대해 통용되는 답을 얻지 못했다.필요에 맞게 수정했습니다.
$cat_name = ''; // I have this set in some shortcodes
if (!isset($cat_name) || $cat_name == '') {
if ( class_exists('WPSEO_Primary_Term') ) {
// Show the post's 'Primary' category, if this Yoast feature is available, & one is set. category can be replaced with custom terms
$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 (is_wp_error($term)) {
$categories = get_the_terms(get_the_ID(), 'category');
$cat_name = $categories[0]->name;
} else {
$cat_name = $term->name;
}
} else {
$categories = get_the_terms(get_the_ID(), 'category');
$cat_name = $categories[0]->name;
}
}
echo $cat_name;
작업 예제를 편집하여 자기 억제력을 높였습니다.커스텀 분류법에 사용했기 때문에 get_the_category() 대신 get_therms()를 사용했습니다.이 코드는 현재 상태로 테스트되지 않았습니다.
언급URL : https://stackoverflow.com/questions/38800626/how-to-get-primary-category-set-via-yoast-seo-plugin-in-wordpress
'programing' 카테고리의 다른 글
| WooCommerce는 현재 카테고리 페이지에서 상위 카테고리를 가져옵니다. (0) | 2023.02.07 |
|---|---|
| HTML 코드를 Wordpress로 표시할 수 있습니까? (0) | 2023.02.07 |
| 필드 수를 알 수 없는 포스트 메타 삭제 및 추가 (0) | 2023.02.07 |
| WordPress 3.5 미디어 업로더 다중 파일 선택 (0) | 2023.02.07 |
| WP Rest API + 각도JS : 페이지에 표시할 Feature Image를 캡처하는 방법 (0) | 2023.02.07 |