2009-06-10 86 views
0

我看了看,並試圖找到我一直在尋找的答案,但我還沒有看到答案爲此:循環設置Wordpress中的帖子數,然後在下一組再次運行相同的循環,等等

我正在嘗試生成一個Wordpress循環,它從一個類別中獲取所有帖子,並在<li></li>標籤內一次顯示三個帖子。

輸出應該是這樣的:

<li>My post title | Another Title | Third title</li> 
<li>The next post title | A different post | Post #6</li> 
<li>And so on | And so forth</li> 

我該類別中的所有條目直到完成需要這個循環,然後退出循環。

我的代碼在這一點上完全不工作,但我已經提供了我正在處理的內容。如果有人有任何解決辦法,我很樂意爲你提供瘋狂的道具,因爲這給我三天沒有任何解決方案。

<?php // Loop through posts three at a time 
$recoffsetinit = '0'; 
$recoffset = '3'; 
query_posts('cat=1&showposts=0'); 
$post = get_posts('category=1&numberposts=3&offset='.$recoffsetinit.'); 
while (have_posts()) : the_post(); 
?> 
<li> 
<?php 
$postslist = get_posts('cat=1&order=ASC&orderby=title'); 
foreach ($postslist as $post) : setup_postdata($post); 
static $count = 0; if ($count == "3") { break; } else { ?> 
<a href="<?php the_permalink() ?>"></a> 
<?php $count++; } ?> 
<?php endforeach; ?> 
<?php $recoffsetinit = $recoffset + $recoffsetinit; ?> 
</li> 
<?php endwhile; ?> 

回答

0

沒有WordPress的測試,沒有時間,但類似這樣的事情可能是一個更好的方式去呢?

<?php 

$postList = get_posts('cat=1&order=ASC&orderby=title'); 
$postsPerLine = 3; 

echo "<ul>"; 
echo buildPosts($postList, $postsPerLine); 
echo "</ul>"; 

function buildPosts($list, $perLine) { 

    $out = ''; 
    $currentPostNumber = 0; 

    foreach ($list as $post) { 

     if ($currentPostNumber == 0) { 
      $out .= '<li>'; 
     } 

     $out .= "<a href='" . the_permalink() . "'></a> "; 

     $currentPostNumber++; 

     if ($currentPostNumber <= $perLine) { 
      $currentPostNumber = 0; 
      $out .= '</li>'; 
     } 

    } 
    return $out; 
} 

?> 
1

我破解了你的解決方案,使其工作。這花了一點時間,因爲我的代碼不是你所說的「好」。這裏的解決方案:

<ul> 
<?php 
query_posts('category=1&showposts=0'); 
$posts = get_posts('category_name=my_cat&order=ASC&orderby=title&numberposts=0'); 
$postsPerLine = 3; 
$currentPostNumber = 0; 

foreach ($posts as $post) : 

    if ($currentPostNumber == 0) { 
      echo '<li>'; 
    } 
      ?> 

    <a href="<?php the_permalink(); ?>"></a> 

    <?php 
$currentPostNumber++; 

    if ($currentPostNumber >= $postsPerLine) { 
      $currentPostNumber = 0; 
      echo '</li>'; 
    } 

    endforeach; 
    ?> 
</ul> 

感謝您的意見!

0

只需立即阻止某個類別的所有帖子,然後遍歷它。創建一個鏈接到每一個職位,在分隔符折騰,並在每三分之一開始一個新的<li>

<ul> 
<?php 
global $post; 
$postsPerLine = 3; 
$counter = 0; 
$myposts = get_posts('category=1&orderby=title&order=ASC'); 

foreach($myposts as $post) : 
    echo (++$counter % postsPerLine) ? : '<li>'; 
?> 
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 
<?php 
    echo ($counter % postsPerLine) ? ' | ' : '</li>'; 
endforeach; 

?> 
</ul> 
相關問題