2010-09-04 36 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