【问题标题】:getting custom taxonomies terms of a post获取帖子的自定义分类术语
【发布时间】:2016-07-01 20:51:30
【问题描述】:

我正在尝试获取我为产品创建的自定义分类的打印名称。

     function create_product_taxonomies()
     {
        // Add new taxonomy, make it hierarchical (like categories)
         $labels = array(
                   'name' => _x('product_categories', 'taxonomy general name'),
                   'singular_name' => _x('Product', 'taxonomy singular name'),
                   'search_items' => __('Search Product Category'),
                   'all_items' => __('All Product Categorie(s)'),
                   'parent_item' => __('Parent Product Category'),
                   'parent_item_colon' => __('Parent Product Category:'),
                   'edit_item' => __('Edit Product Category'),
                   'update_item' => __('Update Product Category'),
                  'add_new_item' => __('Add New'),
                  'new_item_name' => __('New Product Name'),
                  'menu_name' => __('Product Categories'),

                  );

       $args = array(
               'hierarchical' => true,
               'labels' => $labels,
               'show_ui' => true,
               'show_admin_column' => true,
               'query_var' => true,
              'rewrite' => array('slug' => 'product_categories', 'with_front' => true));

      register_taxonomy('product_categories', array('products'), $args);

我已经通过 wordpress 管理面板添加了数据。现在我想在 product.php 文件中显示类别的名称。

    function getLatestProducts()
    { 
       $args = array(
                'post_status' => 'publish',
                'post_type' => 'products', 
                'posts_per_page' => 12, 
               'order' => 'ASC'
             );
      $result = '<div class="col-sm-3">';
      $loop = new WP_Query($args);
      $i=0;
      while ($loop->have_posts()) 
      {
         $loop->the_post();
         $clink=get_permalink($post->ID);
         $desc=get_the_excerpt();
         $categories = get_terms( 'product_categories');
         $desc = strip_tags(str_replace(array("<p>", "</p>"), "", $desc)); 
         $the_imgurl = get_post_custom_values('_cus_n__image');
         $theimage=$the_imgurl[0];
         $the_locurl = get_post_custom_values('_cus_n__location');
         $theloc=$the_locurl[0];
         echo $categories;

         $result .='<div class="product-warp">';
         $result .='<div class="product"> <a href="#"><img src="/wp-content/themes/cake/images/pro1.jpg" title="" alt=""></a> </div>';
         $result .='<div class="product-name">';
         $result .='<h5><a href="#">'.$categories.'</a></h5>';
         $result .='</div>';
         $result .='</div>';
         $i++;

     }
    $result .= '</div>';
    if($i > 0){
      return $result;
    } else {
       return "";
}

}

它只是打印这个arrayarrayarrayarrayarrayarray

【问题讨论】:

  • 好的,兄弟,请告诉我,您还希望当用户点击类别名称时,它会转到一个模板,其中所有产品都显示点击的类别
  • 是的,先生,我当然想这样做
  • 显然您正在尝试打印出一个...,因为您可能已经猜到了一个数组。我不在 wordpress 中工作,但这可以通过 var_dumping $categories 并查看需要访问什么键才能获得正确名称来解决。

标签: php wordpress taxonomy custom-taxonomy


【解决方案1】:

好的,兄弟,您可以为此目的使用get_terms 函数。示例如下:

第一部分

<?php
     $args = array(
                 'orderby' => 'name'
             );
     $terms = get_terms('product_categories', $args);

     foreach($terms as $term) {
?>
         <a href="<?php echo get_term_link($term->slug, 'product_categories') ?>">
             <?php echo $term->name; ?>
         </a>
<?php         
     }
?>

我只给你一个例子。您可以将我的代码粘贴到您想要的位置。

第二部分

现在使用WordPress Taxonomy Template,当用户点击您的一个类别时,下一页会显示点击类别的所有相关产品,您还必须read这个。

如果您阅读了taxonomy Template 链接,那么我们进入下一步。

现在您在主题根文件夹中创建一个文件taxonomy-product_categories.php

这为您的分类创建模板。

现在在这个文件中是完整的代码:

<?php
    get_header();

    $slug = get_queried_object()->slug; // get clicked category slug
    $name = get_queried_object()->name; // get clicked category name

    $tax_post_args = array(
        'post_type' => 'products', // your post type
        'posts_per_page' => 999,
        'orderby' => 'id',
        'order' => 'ASC',
        'tax_query' => array(
            array(
                'taxonomy' => 'product_categories', // your taxonomy
                'field' => 'slug',
                'terms' => $slug
            )
        )
    );
    $tax_post_qry = new WP_Query($tax_post_args);

    if($tax_post_qry->have_posts()) :
       while($tax_post_qry->have_posts()) :
          $tax_post_qry->the_post();

          the_title();

          the_content();

       endwhile;
    endif;

    get_footer();
?>

我再一次告诉你,我只给你一个代码,你可以将这个代码合并到你的主题中。

希望这会对你有所帮助。

【讨论】:

  • 当你成功时告诉我...不要叫我先生
  • 先生。您在第一步中提到的内容。我已经应用了它。它打印整个类别的 8 次,即显示 64 个类别。我只想打印一次
  • hmmmmm 告诉我你想显示所有类别还是只显示 POST 的类别
  • 我只想在产品页面中显示类别的名称和类别的图像。它们是 12 个类别,然后显示 12 个图像和每个图像的标题
  • 它是否对您有帮助,您的图像元术语名称是什么...您在使用 woocommerce
猜你喜欢
  • 1970-01-01
  • 2014-09-04
  • 1970-01-01
  • 2013-06-21
  • 2018-01-08
  • 2012-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多