【问题标题】:Get primary category if more than one is selected?如果选择了多个,则获取主要类别?
【发布时间】:2017-08-24 05:00:36
【问题描述】:

在 Wordpress 中,如何恢复到主要类别?

我正在使用以下循环,如果所有三个都被选中,那么它只会恢复到上一个​​术语。我想确保它是主要类别。

<?php $term_list = wp_get_post_terms($post->ID, 'category', array("fields" => "names"));
foreach ($term_list as $term) {
    $name = $term;
} ?>

【问题讨论】:

  • 将始终在顶部
  • @lazyme114 不一定,可以看到你要去哪里。我想为了安全起见,不应该那样做。
  • 值得一试我在想这个 foreach($term_list as $term) { $name[] = $term; } 回声 $name[0];
  • @lazyme114 我试试看,真的没有太多选择。
  • 这里有一个类似问题的解决方案:https://stackoverflow.com/a/57996959/6036169

标签: php wordpress


【解决方案1】:

对于任何使用Rank Math SEO插件的人,你可以使用:

get_post_meta( $post_id, 'rank_math_primary_category', true );

相关帖子:https://wordpress.org/support/topic/get-primary-category-2/

【讨论】:

    【解决方案2】:

    如果您使用的是“Yoast SEO”插件:

    yoast_get_primary_term_id( 'product_cat', $post-&gt;id );

    【讨论】:

      【解决方案3】:

      如果您使用 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 );
      

      这是正确的做法。

      为了确保在 Yoast 被禁用时网站不会中断,您应该将代码包装在

      if ( class_exists('WPSEO_Primary_Term') ) { }

      【讨论】:

      • 这种方法对我来说适用于较新版本的 Yoast。似乎比为每个检查每个帖子的每个类别都要干净得多。我唯一的补充是包括: if (is_wp_error($term)) { } 这样您就可以判断是否没有主集。
      【解决方案4】:

      如果您使用的是“The SEO Framework”插件而不是“Yoast SEO”:

      $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
         }
      }
      

      参考资料:

      https://github.com/sybrew/the-seo-framework/blob/4262ea703eaaa50813d8cd4ac13f4537b5c6a4cc/inc/classes/post-data.class.php#L633

      【讨论】:

        【解决方案5】:

        这不是原生 wordpress 功能,而是 Yoast SEO (see here) 的一个功能。

        您可以通过以下方式检查主要状态:

        <?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
        

        改为。

        【讨论】:

        • 啊,这可以解释缺乏文档!我会试一试,看看它是否有效。谢谢。
        • 感谢这个有用的代码 sn-p!如果我在获取术语列表时没有错,那么第三个参数应该是数组(“字段”=>“全部”),否则你只会得到所有术语的名称,并且在循环 $term_list 时无法比较 term_id。至少当我尝试时是这样的。
        • @Luckyfella 的评论是正确的 - 上面的代码不会按原样工作。您需要将 array("fields" => "names") 更改为 array("fields" => "all")。您不能将字符串与 ID 值进行比较
        • 感谢通知,已修复。
        • 我认为这个解决方案在新的 Yoast SEO 版本中不再有效。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多