2016-09-15 60 views
0

我使用此代碼來顯示具有特定類的div的WordPress帖子。有沒有辦法以隨機順序獲得帖子,仍然這樣做?我搜索了,找不到任何幫助我弄清楚的東西。謝謝。

<?php $count = 0; if (have_posts()) : while(have_posts()) : the_post() ?>  

<div class="grid4 noall 
<?php if($count%3 == 0) { echo 'first'; }; if($count%3 == 1) { echo 'mid'; }; if($count%3 == 2) { echo 'last'; }; $count++; ?>"> 

<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article"> 

<a class="plink" href="<?php the_permalink() ?>" rel="bookmark" title=" <?php the_title_attribute(); ?>"> 
<section class="portfolio-wrapper"> 

<?php the_post_thumbnail('thumb-800'); ?> 

<div class="portfolio-title"> 
<h3 class="portfolio-header"><?php the_title(); ?></h3> 

<?php if(get_field('portfolio_subtitle')): ?> 
<h5 class="portfolio-category"> 
<?php the_field('portfolio_subtitle'); ?> 
</h5> 
<?php endif; ?> 

</div> 

</section> 
</a> 

</article> 

</div> 

<?php endwhile; ?> 
+0

你能在這裏顯示整個循環,所以我可以看到你當前的查詢? – Fencer04

+0

我更新了代碼 – Lonnie

回答

0

這是在WordPress主循環中,還是使用WP_Query運行自定義查詢?如果是後者,你可以簡單地在你的arguments數組中傳遞'orderby'=>'rand'參數。如果它在主WordPress循環中,答案略有不同,但類似。實質上,你想要將set_query_var函數應用於主循環,有條件地取決於你正在使用哪個頁面/視圖。

https://codex.wordpress.org/Function_Reference/set_query_var

例如,

if(is_post_type_archive('products')){ 
    set_query_var('orderby','rand'); 
} 
+0

這是主循環,我也想在歸檔頁面上使用它。 – Lonnie

相關問題