【问题标题】:How to get the current taxonomy term ID (not the slug) in WordPress?如何在 WordPress 中获取当前的分类术语 ID(不是 slug)?
【发布时间】:2012-08-30 15:06:10
【问题描述】:

我在我的 WordPress 主题文件夹中创建了一个 taxonomy.php 页面。我想获取函数的当前术语 id。 我怎样才能得到这个?

get_query_var('taxonomy') 只返回术语slug,我要ID

【问题讨论】:

    标签: php wordpress taxonomy


    【解决方案1】:

    这适用于所有页面类型,而不仅仅是分类术语档案

    $category_id = get_the_category( get_the_ID());
    $id          = get_category($category_id[0]->term_id);
    

    【讨论】:

      【解决方案2】:

      使用以下代码

      这将打印您当前的分类名称和描述(可选)

      <?php 
         $tax = $wp_query->get_queried_object();
         echo ''. $tax->name . '';
         echo "<br>";
         echo ''. $tax->description .''; 
      ?>
      

      【讨论】:

        【解决方案3】:

        如果您在分类页面中。

        这就是您获取有关分类的所有详细信息的方式。

        get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
        

        这是您获取分类 ID 的方式

        $termId = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) )->term_id;
        

        但如果您在帖子页面(分类 -> 子级)

        $terms = wp_get_object_terms( get_queried_object_id(), 'taxonomy-name');
        $term_id = $terms[0]->term_id;
        

        【讨论】:

          【解决方案4】:

          这是你想要的术语 slug。看起来你可以得到这样的 id 如果这是你需要的:

          function get_term_link( $term, $taxonomy = '' ) {
              global $wp_rewrite;
          
              if ( !is_object($term) ) {
                  if ( is_int( $term ) ) {
                      $term = get_term( $term, $taxonomy );
                  } else {
                      $term = get_term_by( 'slug', $term, $taxonomy );
                  }
              }
          

          【讨论】:

            【解决方案5】:

            简单易行!

            get_queried_object_id()
            

            【讨论】:

            • get_queried_object_id() 是在 3.1.0(2011 年 2 月 23 日)中引入的,应该是所提问题的正确答案。 > taxonomy.php 页面在我的 wordpress 模板文件夹中,我想获取当前的 term id 用于功能。
            【解决方案6】:

            wp_get_post_terms(),你会这样做:

            global $post;
            $terms = wp_get_post_terms( $post->ID, 'YOUR_TAXONOMY_NAME',array('fields' => 'ids') );
            
            print_r($terms);
            

            【讨论】:

              【解决方案7】:
              <?php 
              $terms = get_the_terms( $post->ID, 'taxonomy');
              foreach ( $terms as $term ) {
                  $termID[] = $term->term_id;
              }
              echo $termID[0]; 
              ?>
              

              【讨论】:

                【解决方案8】:

                这是 sn-p 所需的全部代码:

                $queried_object = get_queried_object();
                $term_id = $queried_object->term_id;
                

                【讨论】:

                  【解决方案9】:

                  没关系!我找到了:)

                  get_queried_object()->term_id;
                  

                  【讨论】:

                  • 您也可以只使用get_queried_object_id() 来检索ID。整个 sn-p 将是 $term_id = get_queried_object_id();
                  • 好答案。最近我在 WordPress 堆栈交换中看到一个问题。请检查一下。 wordpress.stackexchange.com/questions/214453/…
                  • 请记住,这仅适用于分类页面。但是,如果您在分类子页面中,您将不会获得分类 ID。
                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-11-25
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2018-04-20
                  • 2021-09-07
                  相关资源
                  最近更新 更多