首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Yoast SEO插件设置主类别

使用Yoast SEO插件设置主类别
EN

WordPress Development用户
提问于 2018-09-19 15:26:48
回答 2查看 6.1K关注 0票数 0
代码语言:javascript
运行
复制
function set_primary_on_publish ($post_id) {
    global $post; 

    $categories = get_the_terms( $post->ID, 'category' ); 
    // wrapper to hide any errors from top level categories or products without category
    if ( $categories && ! is_wp_error( $category ) ) : 

        // loop through each cat
        foreach($categories as $category) :
          // get the children (if any) of the current cat
          $children = get_categories( array ('taxonomy' => 'category', 'parent' => $category->term_id ));

          if ( count($children) == 0 ) {

                $childid = $category->term_id;
                update_post_meta($post->ID,'_yoast_wpseo_primary_category',$childid);

          }
        endforeach;

    endif;

    }
add_action( 'save_post', 'set_primary_on_publish', 10, 2 );
add_action( 'edit', 'set_primary_on_publish', 10, 2 );
add_action( 'wp_insert_post', 'set_primary_on_publish', 10, 2 );

我试图保存最深的子类作为主要类别在Yoast SEO插件,但上述似乎没有做的工作。

有人能帮我解决这个问题吗?

我向你问好,迪伦·斯米特

EN

回答 2

WordPress Development用户

发布于 2019-01-17 04:35:22

试一试

代码语言:javascript
运行
复制
update_post_meta($post->ID,'_yoast_wpseo_primary_category',$childid); 

使用以下功能:

代码语言:javascript
运行
复制
function wpseoPrimaryTerm($taxonomy, $postID, $term){

  if ( class_exists('WPSEO_Primary_Term') ) {
  // Set primary term.
  $primaryTermObject = new WPSEO_Primary_Term($taxonomy, $postID);
  $primaryTermObject->set_primary_term($term);

  // Save primary term.
  $primaryTermObjectAdmin = new WPSEO_Primary_Term_Admin();
  $primaryTermObjectAdmin->save_primary_terms($postID);
  }else{
    echo 'Class WPSEO does not exit';
  }
}

其中$taxonomy - taxonomy name, $PostID - $post->ID, $term - $childid

票数 1
EN

WordPress Development用户

发布于 2019-07-24 16:50:49

您需要将product更改为product_cat,将_yoast_wpseo_primary_category更改为_yoast_wpseo_primary_product_cat以使其正常工作。在保存或更新产品时,它将获取最深的product_id,并将其设置为wp_postmeta表中的product_id

代码语言:javascript
运行
复制
function set_primary_on_publish ($post_ID) {
    global $post;

    $categories = get_the_terms( $post->ID, 'product_cat' ); 

    // wrapper to hide any errors from top level categories or products without category
    if ( $categories && ! is_wp_error( $category ) ) :

        // loop through each cat
        foreach($categories as $category) :

          // get the children (if any) of the current cat
          $children = get_categories( array ('taxonomy' => 'product_cat', 'parent' => $category->term_id ));

          if ( count($children) == 0 ) {
                $childid = $category->term_id;
                update_post_meta($post->ID,'_yoast_wpseo_primary_product_cat',$childid);
          }
        endforeach;
    endif;
}
票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/314633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档