簡體   English   中英

在WordPress Loop上隨機訂購帖子

[英]Randomly Order Posts on WordPress Loop

我有以下代碼,顯示5個帖子,每個帖子都是作者的最新文章。 我想要做的是以隨機順序顯示這5個帖子,以使沒有作者能優先於其他帖子。 為了澄清這一點,是這5個帖子的順序,而不是作者的帖子。 謝謝

碼:

<?PHP

  get_header();

 ?>


 <script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/jquery.masonry.js"></script>
 <script type="text/javascript">
  jQuery(document).ready(function(){
   $('#post-list').masonry({ singleMode: true, itemSelector: 'article', animate: false });
  });
 </script>

 <?php

  function MyLoopCode()
  {
 ?>

 <article id="post-<?php the_ID(); ?>">

  <div class="post-image"></div>

  <div class="post-text">

  <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

  <p class="p-cat">In: <?php the_category('|') ?></p>

  <p class="p-author">
   <span class="name"><?php the_author_posts_link(); ?></span>
   <span class="avatar"><a title="View posts by <?php the_author(); ?>" href="<?php echo get_author_posts_url($authordata->ID); ?>"><?php echo get_avatar( $email, $size = '64' ); ?></a>
   </span>
  </p>


  <small class="p-time">
  <strong class="day"><?php the_time('j') ?></strong>
  <strong class="month"><?php the_time('M') ?></strong>
  <strong class="year"><?php the_time('Y') ?></strong>
  </small>

  <section class="content">
   <?php the_content('<p>Read the rest of this page &raquo;</p>'); ?>
  </section>

  <div class="p-det">
   <p class="p-det-com"><?php comments_popup_link('No Comments', '(1) Comment', '(%) Comments'); ?></p>
   <?php if (function_exists('the_tags')) { ?> <?php the_tags('<p class="p-det-tag">Tags: ', ', ', '</p>'); ?> <?php } ?>
  </div>

  </div>

 </article>

 <?php } ?>



   <div id="maincontent" class="clearfix">

    <div class="leftcontent">

     <section id="post-list" class="post-list">

     <?php //query_posts('orderby=rand'); ?>

     <?php query_posts('posts_per_page=1&author=2'); if (have_posts()) : while (have_posts()) : the_post(); ?>

      <?php echo MyLoopCode(); ?>

     <?php endwhile; endif; ?>

     <?php rewind_posts(); ?>

     <?php query_posts('posts_per_page=1&author=3'); if (have_posts()) : while (have_posts()) : the_post(); ?>

      <?php echo MyLoopCode(); ?>

     <?php endwhile; endif; ?>

     <?php rewind_posts(); ?>

     <?php query_posts('posts_per_page=1&author=4'); if (have_posts()) : while (have_posts()) : the_post(); ?>

      <?php echo MyLoopCode(); ?>

     <?php endwhile; endif; ?>

     <article>

      <p>ADVERTISEMENT</p>

     </article>

     <?php rewind_posts(); ?>

     <?php query_posts('posts_per_page=1&author=5'); if (have_posts()) : while (have_posts()) : the_post(); ?>

      <?php echo MyLoopCode(); ?>

     <?php endwhile; endif; ?>

     <?php rewind_posts(); ?>

     <?php query_posts('posts_per_page=1&author=6'); if (have_posts()) : while (have_posts()) : the_post(); ?>

      <?php echo MyLoopCode(); ?>

     <?php endwhile; endif; ?>

     </section>

    </div>
    <!-- END div.leftcontent -->

    <?php get_sidebar(); ?>

   </div>
   <!-- END div#maincontent -->



 <?PHP

  get_footer();

 ?>

摘要:
1.獲取MyLoopCode()輸出作為數組。
2.隨機排列陣列。
3.顯示內容。


實現方式:

1)使用ob_start()ob_get_clean() )返回MyLoopCode()的輸出,並將其存儲在數組中。

說明: ob_start()開始輸出緩存,因此PHP會將輸出保留在其緩沖區中,而不是將輸出發送到瀏覽器。 然后,在函數的最后,通過使用ob_get_clean() ,我們告訴PHP將輸出作為字符串使用,並從其緩沖區中刪除。 因此,該函數現在返回MyLoopCode()函數將以其他方式輸出到瀏覽器的內容。

<?php
 function MyLoopCode()
  {
ob_start();
 ?>

 <article id="post-<?php the_ID(); ?>">

  <div class="post-image"></div>

  <div class="post-text">

  <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

  <p class="p-cat">In: <?php the_category('|') ?></p>

  <p class="p-author">
   <span class="name"><?php the_author_posts_link(); ?></span>
   <span class="avatar"><a title="View posts by <?php the_author(); ?>" href="<?php echo get_author_posts_url($authordata->ID); ?>"><?php echo get_avatar( $email, $size = '64' ); ?></a>
   </span>
  </p>


  <small class="p-time">
  <strong class="day"><?php the_time('j') ?></strong>
  <strong class="month"><?php the_time('M') ?></strong>
  <strong class="year"><?php the_time('Y') ?></strong>
  </small>

  <section class="content">
   <?php the_content('<p>Read the rest of this page &raquo;</p>'); ?>
  </section>

  <div class="p-det">
   <p class="p-det-com"><?php comments_popup_link('No Comments', '(1) Comment', '(%) Comments'); ?></p>
   <?php if (function_exists('the_tags')) { ?> <?php the_tags('<p class="p-det-tag">Tags: ', ', ', '</p>'); ?> <?php } ?>
  </div>

  </div>

 </article>

 <?php
  return ob_get_clean();
  } ?>

2)現在,就像我所說的那樣,不要直接echo顯輸出,而是將其保存在數組中:

說明:每次MyLoopCode()函數時,其輸出現在都存儲在數組$myarray 因此,尚無輸出發送到瀏覽器。

<?php query_posts('posts_per_page=1&author=2'); if (have_posts()) : while (have_posts()) : the_post(); ?>

  <?php $myarray[] = MyLoopCode(); ?>

<?php endwhile; endif; ?>

在所有這些函數調用之后, $myarray的內容將類似於(偽代碼):

myarray[0] = user1-post1 + user1-post2 + user1-post3 + user1-post4 + user1-post5;  
myarray[1] = user2-post1 + user2-post2 + user2-post3 + user2-post4 + user2-post5;  
myarray[2] = user3-post1 + user3-post2 + user3-post3 + user3-post4 + user3-post5;  
myarray[3] = user4-post1 + user4-post2 + user4-post3 + user4-post4 + user4-post5;  
myarray[4] = user5-post1 + user5-post2 + user5-post3 + user5-post4 + user5-post5;  

3)現在,使用shuffle()隨機化數組內容並顯示它們:

說明: shuffle()函數將$myarray的內容隨機化。 由於此數組包含單個用戶的所有帖子,因此實際發生的是帖子用戶組是隨機的。 最后,通過foreach遍歷數組並回顯內容。

 <?php
  shuffle($myarray);

  foreach($myarray as $x)
        echo $x;
 ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM