从分类中获取所有帖子

时间:2013-12-04 16:01:34

标签: php wordpress custom-post-type taxonomy

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

示例:

工作室:

-list property

-list property

-list property

-list property

/**
 * 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 );

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

谢谢,像魅力一样工作

这是我的代码:

<?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().

?>