如何按自定义分类显示自定义帖子类型


How to display custom post type by custom taxonomy?

我的主页正在按照我输入的顺序破坏我的列表(我的自定义帖子类型)。我希望具有自定义分类"标签"(特别优惠)的列表显示在我主页的第一页上,然后其余列表与以前完全相同。我是wordpress的新手,希望我问对了我的问题

这是我的主页代码

<div id="content">
<?php include (TEMPLATEPATH . '/lib/slider.php'); ?>    
<?php
$args = array(
'post_type' => 'listings',
'paged' => $paged,
'showposts' => 8 ,
'oferta' =>"oferta"
);
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query($args);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>   
<div class="post propbox <?php if (++$counter % 2 == 0) { echo "lastbox"; }?> clearfix" id="post-<?php the_ID(); ?>">
<div class="archimg">
<?php  if( has_term( 'featured', 'type', $post->ID ) ) { ?>
<span class="featspan">Featured</span>
<?php } else if ( has_term( 'sold', 'type', $post->ID ) ){ ?>
<span class="soldspan">Sold</span>
<?php } else if ( has_term( 'reduced', 'type', $post->ID ) ){ ?>
<span class="redspan">Reduced</span>
<?php } ?>
<?php
    if ( has_post_thumbnail() ) { ?>
    <a href="<?php the_permalink() ?>"><img class="propimg" src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php get_image_url(); ?>&amp;h=180&amp;w=310&amp;zc=1" alt=""/></a>
        <?php } else { ?>
    <a href="<?php the_permalink() ?>"><img class="propimg" src="<?php bloginfo('template_directory'); ?>/images/dummy.jpg" alt="" /></a>
<?php } ?>
</div>
<div class="cover">
    <div class="title">
        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
    </div>
    <div class="propmeta">
    <div class="proplist"><span>Price</span> <span class="propval"> <?php $price=get_post_meta($post->ID, 'wtf_price', true); echo $price; ?></span></div>
    <div class="proplist"><span>Location</span> <span class="propval"> <?php echo get_the_term_list( $post->ID, 'location', '', ' ', '' ); ?></span></div>
    <div class="proplist"><span>Property type</span> <span class="propval"><?php echo get_the_term_list( $post->ID, 'property', '', ' ', '' ); ?></span></div>
    <div class="proplist"><span>Area</span> <span class="propval"> <?php echo get_the_term_list( $post->ID, 'area', '', ' ', '' ); ?></span></div>
    </div>
    <div class="entry">
        <?php wpe_excerpt('wpe_excerptlength_archive', ''); ?>
        <a class="morer" href="<?php the_permalink() ?>">Check this</a>
        <div class="clear"></div>
    </div>
</div>
</div>
<?php endwhile; ?>
<div class="clear"></div>
<?php getpagenavi(); ?>
<?php $wp_query = null; $wp_query = $temp;?>
</div>

这是我的主要内容内容对不起,我该如何重新调整它以适合我的需求

你可以像这里一样使用tax_query

$args = get_posts( array(
    'post_type' => 'my_post_type',
    'tax_query' => array(
        array(
            'taxonomy' => 'my_taxonomy',
            'field' => 'slug',
            'terms' => 'webdesign'
        )
    )
) );
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query($args);

你必须将参数数组传递给你$wp_query->query($args);在其中定义标签名称或类别名称、标签的分类或类别的分类

$args = array(
'post_type' => 'your custom post type name',
'paged' => $paged,
'showposts' => 8 ,
'your custom goes here taxonomy' =>"tag name or category name"
);
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query($args);

通常,分类名称是您在register_taxonomy('here_goes_taxonomy',array('post_type'), array(...))中定义的名称

上面的所有答案都很棒,但你不一定需要传递tax_query数组。以正确的方式创建自定义分类后,可以执行以下操作。

<?php
$paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
$loop = new WP_Query([
 'post_type'            => 'your_custom_post_type',
 'post_status'          => 'publish', // fetch only published posts
 'posts_per_page'       => get_option('posts_per_page') ?: 10, // number of posts to fetch
 'paged'                => $paged
 'your_custom_taxonomy' => 'your_custom_taxonomy_slug_or_name'
]);
// WordPress pagination doesn't work on custom query
// Let's make it work
$temp_query = $wp_query; // store $wp_query in a temporary variable
$wp_query   = null; // set it to null
$wp_query   = $loop; // store your custom query to $wp_query

if ( $loop->have_posts() ) { // Let's check if we have some posts
    while( $loop->have_posts() ){ 
     $loop->the_post();
        // add your markup here
     }
 }
 wp_reset_postdata();
 $wp_query = null;
 $wp_query = $temp_query;  // store back the original $wp_query
                         <?php  $cat_terms = get_terms(
                                      array('mobile_category'),
                                        array(
                    'hide_empty'    => false,
                    'orderby'       => 'name',
                    'order'         => 'ASC',
                    'number'        => 2 ) );
                        if( $cat_terms ) :  ?>
             
                    
                             <?php  foreach( $cat_terms as $term ) :
                                   $args = array(
                                            'post_type'=> 'mobile',
                                             'posts_per_page'=> 2, 
                                              'post_status'=> 'publish',
                                    'tax_query'=> array(
                                            array(
                                               'taxonomy' => 'mobile_category',
                                                'field'  => 'slug',
                                                 'terms' => $term->slug,),  ),
                                                  'ignore_sticky_posts' => true 
                                                   );
                                     $_posts = new WP_Query( $args );
                         if( $_posts->have_posts() ) :
                        while( $_posts->have_posts() ) : $_posts->the_post(); ?>
                            <span class="color2"><?php echo $term->name ; ?></span>
                                    
                      
                            <?php   endwhile;
                                        endif;
                                  wp_reset_postdata(); 
                             endforeach; ?>
                        
                <?php endif; ?>