簡體   English   中英

獲取分類中的所有帖子

[英]Get all posts from a taxonomy

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

范例:

工作室:

-list屬性

-list屬性

-list屬性

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

使用稅收查詢( http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

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

謝謝,像個魅力一樣工作

這是我的代碼:

<?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