【问题标题】:Get all posts from a taxonomy从分类中获取所有帖子
【发布时间】:2013-12-04 16:01:34
【问题描述】:

我如何列出分类中的所有帖子,例如我有“工作室”的示例

例子:

工作室:

-列表属性

-列表属性

-列表属性

-列表属性

/**
 * Custom taxonomies
 */
function aviators_properties_create_taxonomies() {


    $property_types_labels = array(
        'name'              => __( 'Property Types', 'aviators' ),
        'singular_name'     => __( 'Property Type', 'aviators' ),
        'search_items'      => __( 'Search Property Types', 'aviators' ),
        'all_items'         => __( 'All Property Types', 'aviators' ),
        'parent_item'       => __( 'Parent Property Type', 'aviators' ),
        'parent_item_colon' => __( 'Parent Property Type:', 'aviators' ),
        'edit_item'         => __( 'Edit Property Type', 'aviators' ),
        'update_item'       => __( 'Update Property Type', 'aviators' ),
        'add_new_item'      => __( 'Add New Property Type', 'aviators' ),
        'new_item_name'     => __( 'New Property Type', 'aviators' ),
        'menu_name'         => __( 'Property Type', 'aviators' ),
    );

    register_taxonomy( 'property_types', 'property', array(
        'labels'       => $property_types_labels,
        'hierarchical' => true,
        'query_var'    => 'property_type',
        'rewrite'      => array( 'slug' => __( 'property-type', 'aviators' ) ),
        'public'       => true,
        'show_ui'      => true,
    ) );

}

add_action( 'init', 'aviators_properties_create_taxonomies', 0 );

【问题讨论】:

    标签: php wordpress custom-post-type taxonomy


    【解决方案1】:

    使用税务查询 (http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters)

                 $args['tax_query'] = array(
                    array(
                        'taxonomy'  => 'property_types'
                        ,'field'    => 'slug'
                        ,'terms'    => 'your_slug'
                    )
                );
    

    【讨论】:

      【解决方案2】:

      谢谢,工作就像一个魅力

      这是我的代码:

      <?php
      $args=array(
        'post_type' => 'property',
        'taxonomy' => 'property_types',
        'caller_get_posts'=> 0,
        'tax_query' => array(
          array(
              'taxonomy' => 'property_types',
              'terms' => 'rooms',
              'field' => 'slug',
              'include_children' => true,
              'operator' => 'IN'
          )
      ),
      );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
      
      
              <?php
        endwhile;
      }
      wp_reset_query();  // Restore global post data stomped by the_post().
      
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-20
        • 1970-01-01
        • 2012-04-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-11
        相关资源
        最近更新 更多