Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

The function below lists posts based on custom field.

The result is involved in a list <li></li>. What I would like to do is count how many list items there are, then divide it into two ul's. Is there any way to split the result into two columns?

function article_series() {
global $post;
$series = get_post_meta($post->ID, 'Series', true);
if($series) :
    $args = array(
        'numberposts' => -1,
        'meta_key' => 'Series',
        'meta_value' => $series,
    );
    $series_posts = get_posts($args);
    if($series_posts) :
        $class = preg_replace("/[^a-z0-9\\040\\.\\-\\_\\\\]/i", "", $series);
        $class = strtolower(str_replace(array(' ', '&nbsp;'), '-', $class));
        echo '<div class="series series-' . $class . '"><h4 class="series-title">' . __('Articles in this series') . '</h4><ul>';
        foreach($series_posts as $serial) :
            if($serial->ID == $post->ID)
                echo '<li class="current-post">' . $serial->post_title . '</li>';
            else
                echo '<li><a href="' . get_permalink($serial->ID) . '" title="' . str_replace('"', '"', $serial->post_title) . '">' . str_replace('"', '"', $serial->post_title) . '</a></li>';
        endforeach;
        echo '</ul></div>';
    endif;
endif;

Function created by justintadlock.com

Thanks for any help.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

You could do this with the modulo operator.

$posts = get_posts($args);
$html = '<ul>';
$limit = 5;
$i = 1;

foreach ($posts as $post) {
    $html .= '<li>' . $post->post_title . '</li>';

    if($i % $limit == 0) {
        $html .= '</ul><ul>';
    }

    $i++;
}

$html .= '</ul>';
echo $html;
share|improve this answer
    
Great @rofflox. Modulo operation seems to have worked here. –  BóbGCA Jul 18 '12 at 16:29
add comment

You'll want to look at the number of series posts and stick an end/start <ul> in the middle. Something like:

echo '<ul class="left">';
$series_count = count($series_posts);
$series_middle = ceil($series_count / 2);
for ($i = 0; $i < $series_count; $i++) {
    if ($serial->ID == $post->ID)
        echo '<li class="current-post">' . $serial->post_title . '</li>';
    else
        echo '<li><a href="' . get_permalink($serial->ID) . '" title="' . str_replace('"', '"', $serial->post_title) . '">' . str_replace('"', '"', $serial->post_title) . '</a></li>';

    if ($i == $series_middle)
        echo '</ul><ul class="right">';
}
echo '</ul>';

Then you can use CSS to style the two lists to appear next to each other.

share|improve this answer
    
Thanks @Nate Cook, but there seems to be a bug, because it adds <ul> for each item returned,. Returned 3 results looked like this: <ul class="left"><li>Result 1</li></ul><ul class="right"><li>Result 2</li></ul><ul class="right"><li>Result 3</li></ul><ul class="right">ul empty</ul>. –  BóbGCA Jul 18 '12 at 3:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.