首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在WordPress循环上随机排序帖子

问在WordPress循环上随机排序帖子
EN

Stack Overflow用户
提问于 2010-09-04 18:36:06
回答 1查看 984关注 0票数 0

我有下面的代码,其中显示了5个帖子,每个帖子都是作者的最新帖子。我想要做的是以随机的顺序显示这5个帖子,这样没有作者会优先于其他作者。要澄清的是,这是这5篇文章的顺序,而不是作者的文章。谢谢

代码:

代码语言:javascript
复制
<?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();

 ?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-09-04 20:17:36

摘要:

  1. 以数组的形式获取MyLoopCode()的输出。
  2. 对数组进行置乱。
  3. 显示内容。

Implementation:

1)使用ob_start()和ob_get_clean()返回MyLoopCode()的输出,并将其存储在数组中。

说明:ob_start()会启动输出缓存,因此不会将输出发送到浏览器,而是会将输出保存在其缓冲区中。然后,在函数的末尾,通过使用ob_get_clean(),我们告诉PHP将输出作为字符串使用,并从缓冲区中删除。因此,该函数现在返回原本由MyLoopCode()函数输出到浏览器的内容。

代码语言:javascript
复制
<?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中。因此,还没有输出发送到浏览器。

代码语言:javascript
复制
<?php query_posts('posts_per_page=1&author=2'); if (have_posts()) : while (have_posts()) : the_post(); ?>

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

<?php endwhile; endif; ?>

在所有这些函数调用之后,$myarray的内容将如下所示(伪代码):

代码语言:javascript
复制
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遍历数组并回显内容。

代码语言:javascript
复制
 <?php
  shuffle($myarray);

  foreach($myarray as $x)
        echo $x;
 ?>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3641871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档