2010-09-04 34 views
0

我下面的代码显示了5个帖子,每个帖子都是来自作者的最新版本。想要我想要做的就是以随机顺序显示这5个帖子,以便没有作者优先于其他作者。澄清这是这5个帖子的排序,而不是作者的帖子。感谢随机订购WordPress循环上的帖子

代码:

<?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(); 

?> 
+0

打算建议使用'ORDER BY RAND()'在那里的某个地方,但是我看到这已经在某个时候被添加/注释掉了。 – 2010-09-04 10:42:29

+0

是的,当你调用多个查询时,它不起作用,因为我没有订购查询结果,而是自己排序查询。 – Cameron 2010-09-04 10:50:25

回答

1

总结:
1的MyLoopCode()获取输出为一个数组。
2.随机阵列。
3.显示内容。


实现:

1)返回的MyLoopCode()使用ob_start()ob_get_clean() &存储在它的阵列的输出。

说明: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。所以,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的内容。由于这个数组包含了个人用户的所有帖子,真正发生的是帖子用户组是随机的。最后通过foreach遍历数组并回显内容。

<?php 
    shuffle($myarray); 

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

我不太确定这是如何工作的,但它似乎在做伎俩。你能否更详细地解释每条线的作用,以便我可以从中学习。谢谢。 – Cameron 2010-09-04 13:39:20

+0

@Cameron,我已经解释了每一步。如果这适用于您,请注册并接受答案。 – shamittomar 2010-09-04 14:05:22