gpt4 book ai didi

php - WordPress 按帖子类型对帖子进行排序

转载 作者:行者123 更新时间:2023-12-02 04:21:16 25 4
gpt4 key购买 nike

我正在尝试创建一个从两种帖子类型获取帖子的系统,并且我想按帖子类型显示帖子顺序。我试图首先显示一种帖子类型的帖子,然后显示另一种帖子类型的帖子,但我使用的查询将其全部混合。任何人都可以为此提出一个好的解决方案吗?这是我的代码。

    $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(

'post_type' => array('review','directory'),
'orderby' => 'name',
'order' => 'ASC',
'posts_per_page' => '4',
'paged' => $paged ,
'tax_query' => array( array(
'taxonomy' => 'Reviews',
'field' => 'id',
'terms' => $cat_id
), ),
);

query_posts($args);
if (have_posts()) :
while (have_posts()) : the_post();
$product_terms = wp_get_post_terms(get_the_ID(), 'Reviews', array("fields" => "all", "order" => "DESC"));
$postype=get_post_type( get_the_ID() );
if($postype=='review')
{
?>

<div class="review-post">
<a href="http://<?php the_permalink(); ?>" target="_blank"><h3><?php the_title(); ?></h3></a>
<div class="review-cat-new">
<?php echo get_the_term_list( $post->ID, 'Reviews', 'Category: ', ', ', '' ); ?>
</div>

<?php
if(get_field('see_it'))
$seeit_text= get_field('see_it');
if(get_field('skip_it'))
$skipit_text= get_field('skip_it');
?>
<div class="see-skip">
<p class="see-it"><span>See it:</span>
<?php echo $seeit_text; ?>
</p>
<p class="skip-it"><span>Skip it:</span>
<?php echo $skipit_text; ?>
</p>
</div>
<?php echo custom_field_excerpt(); ?>
</div>
<?php }
else
{
?>
<div class="review-post">
<a href="h<?php the_permalink(); ?>" target="_blank"><h3><?php the_title(); ?></h3></a>
<div class="review-cat-new">
<?php echo get_the_term_list( get_the_ID(), 'Reviews', 'Category: ', ', ', '' ); ?>
</div>


<?php echo the_field('enter_content_direc'); ?>
<?php
if(get_field('enter_textdirec'))
$text= get_field('enter_textdirec');
if(get_field('enter_linkdirec'))
$textlink= get_field('enter_linkdirec');
?>
<div class="see-skip">
<p class="see-it direc"><span><a target="_blank" style="color:#5D6D71; text-transform: lowercase;" href="<?php echo $textlink;?>"><?php echo $textlink;?> </a></span>

</p>
</div>
</div>
<?php }
endwhile;
echo '<div class="paging">';
wp_pagenavi();
echo '</div>';
endif;

有没有办法让我可以先显示评论形式的帖子,然后显示目录?

最佳答案

如文档 https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters 中所示,您可以通过将数组传递给 'orderby' 来使用常规 wp 查询:

$wp_query = new WP_Query(
array(
's' => $search,
'orderby'=> array('type'=>'DESC', 'title'=>'ASC'),
'paged'=>$paged
)
);
if ( $wp_query->have_posts() ) {
while ( $wp_query->have_posts() ) {
$wp_query->the_post();
// do your loop stufff here
}
}

关于php - WordPress 按帖子类型对帖子进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15675296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com