0

can anyone help me split this custom post type into two lists? i'm a novice PHPr but have been left with this code by someone who said it was too complicated...

function listforcontinent($name, $top = false){
    // top=true - show name, no columns
    // top=false - dont show name, columns

    $slugname = sanitize_title($name);
    $args = array('tax_query' => array(array(

                                             'taxonomy' => 'jurisdiction_continents',
                                             'field' => 'slug',
                                             'terms' => $slugname)), 
                                             'post_type' => 'jurisdiction',
                                             'orderby' => 'title', 
                                             'posts_per_page' => 35,
                                             'order' => 'ASC');

    $postslist = get_posts( $args );
    if ($postslist) {
        if($top) echo '<h4>'.$name.'</h4>';
        echo '<ul>';
        foreach ( $postslist as $post ) {
            setup_postdata($post);
            echo '<li><a href="'.get_permalink($post->ID).'">'.get_the_title($post->ID)."</a></li>";
        }
        echo '</ul>';
    }else{
        if($top) echo '<h4>'.$name.'</h4>';
    }                               
}

any help would be most appreciated, thanks :)

  • You want to split into columns by what criteria? Half of the results per column? – s_ha_dum Jan 29 '13 at 18:48
0

You could count the number of items and insert </ul><ul> after 50%.

Sample code, not tested:

if ($postslist) {

    $count = 1;

    if($top) echo '<h4>'.$name.'</h4>';
    echo '<ul>';
    foreach ( $postslist as $post ) {
        echo '<li><a href="'.get_permalink($post->ID).'">'.get_the_title($post->ID)."</a></li>";
        if ( 0 === $count % 10 )
            echo '</ul><ul>';

        $count += 1;
    }
    echo '</ul>';
}

And you don’t need setup_postdata($post); here.

  • this doesn't quite work but thanks for giving it a go. i need it to split after a certain number of items say 10, otherwise a list with 2 will split, when i'd rather it just stayed in one column. – user26724 Jan 29 '13 at 19:27
  • @user26724 See my update. – fuxia Jan 29 '13 at 19:33
  • I apologise, this actually worked perfectly, i was being dumb :) thank you! – user26724 Jan 29 '13 at 21:11
0

to avoid getting an empty unordered list if there are exact multiples of 10 list items, and to split the posts list in half regardsless of its length, but have at least 10 items in the first unordered list, try this code:

if ($postslist) {

    $count = 0; 
    $half = max( 10, ceil( count( $postslist )/2 ) );

    if($top) echo '<h4>'.$name.'</h4>';
    echo '<ul>';
    foreach ( $postslist as $post ) {

        if ( $count != 0 && 0 === $count % $half )
            echo '</ul><ul>';

            echo '<li><a href="'.get_permalink($post->ID).'">'.$post.get_the_title($post->ID)."</a></li>";
        $count += 1;

         }
    echo '</ul>';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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