我有以下代码,显示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 »</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();
?>
答案 0 :(得分:1)
<强>要点:强>
1.获取MyLoopCode()
的数组作为数组
2.随机播放阵列
3.显示内容。
<强>实施强>
1)使用ob_start()
和ob_get_clean()
&amp;返回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 »</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
中。因此,NO输出尚未发送到浏览器。
<?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
的内容。由于此数组包含单个用户的所有帖子,因此实际发生的事情是posts-user组是随机的。最后通过foreach遍历数组并回显内容。
<?php
shuffle($myarray);
foreach($myarray as $x)
echo $x;
?>