views:

618

answers:

4

I have looked and looked and tried to find an answer for what I've been looking for, but I have yet to see an answer for this:

I am trying to generate a Wordpress loop that takes all the posts from a single category and displays them three at a time inside <li></li> tags.

Output should look like this:

<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>

I need this to loop through all the entries in the category until finished, and then exit the loop.

My code is completely non-working at this point, but I've provided what I'm working with below. If anyone has any solution to this, I'd love to give mad props to you, as this has hounded me for three days without any solution so far.

<?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; ?>
A: 

No wordpress to test with, and no time, but something like this might be a better way of going about it?

<?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  A: 

I hacked up your solution to make it work. It took a little doing, since my code-fu is not what you'd call "good." Here's the solution:

<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>

Thanks for the input!

f8xmulder
A: 

Just snag all the posts for a category, at once, then iterate over it. Create a link to every post, toss in the separator, and on every third post start a new <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>
Curtis Tasker
A: 

Very useful information. Will be using this technique, thanks for this and bless you all x

Greg Shaw