简体   繁体   English

获取分类中的所有帖子

[英]Get all posts from a taxonomy

How can I list all post from a taxonomy like example i have "studios" 如何列出分类中的所有帖子,例如我拥有“工作室”的示例

Example : 范例:

Studios: 工作室:

-list property -list属性

-list property -list属性

-list property -list属性

-list property -list属性

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

Use tax query ( http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters ) 使用税收查询( http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

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

Thanks, work like a charm 谢谢,像个魅力一样工作

this is my code: 这是我的代码:

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

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM