0

我的主页按我输入的顺序显示我的列表(我的自定义帖子类型)。我希望具有自定义分类法“标签”(特价)的列表显示在我主页的第一页上,然后其余列表与之前的位置完全相同。我是 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>

这是我的主要内容内容对不起我怎样才能重新调整它以符合我的需要

4

4 回答 4

6

你可以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);
于 2013-09-07T11:55:23.070 回答
3

您必须将参数数组传递给您$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(...))

于 2013-09-07T10:07:37.817 回答
0
                         <?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; ?>
于 2021-07-09T08:36:31.403 回答
0

上面的所有答案都很好,但您不一定需要传递 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
于 2021-05-04T11:08:51.010 回答