• Resolved mattregister

    (@mattregister)


    I am trying to display a list of termsfrom my taxonomy “organization” in an unordered list but display them in two columns. This is what I have for my first column:

    function print_organizations1() {
    $countterms = wp_count_terms( 'organization' );
    if($countterms&1) {
    $firstcolumn = ( ( ( $countterms - 1) / 2 ) + 1);
    } else {
    $firstcolumn = ( $countterms  / 2 ); }
    $secondcolumn = ( $countterms - $firstcolumn );
    $terms = get_terms( 'organization', array('number' => $firstcolumn ));
    print '<div class="vcex-bullets vcex-bullets-gray"><ul>';
    foreach ( $terms as $term ) {
         // The $term is an object, so we don't need to specify the $taxonomy.
        $term_link = get_term_link( $term );
        // If there was an error, continue to the next term.
        if ( is_wp_error( $term_link ) ) {
            continue;
        }
        // We successfully got a link. Print it out.
        print '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>';
    }
    print '</ul></div>';
    }
    add_shortcode('print-organizations1', 'print_organizations1');

    That works perfectly. It figures out how many is in the first half of the list and uses that number of terms to display. When I try to make the second column is where I am getting lost. I try to offset the list by the number of terms in the first column.

    function print_organizations2() {
    $countterms = wp_count_terms( 'organization' );
    if($countterms&1) {
    $firstcolumn = ( ( ( $countterms - 1) / 2 ) + 1);
    } else {
    $firstcolumn = ( $countterms  / 2 ); }
    $secondcolumn = ( $countterms - $firstcolumn );
    $terms = get_terms( 'organization', array('offset' => $firstcolumn ));
    print '<div class="vcex-bullets vcex-bullets-gray"><ul>';
    foreach ( $terms as $term ) {
         // The $term is an object, so we don't need to specify the $taxonomy.
        $term_link = get_term_link( $term );
        // If there was an error, continue to the next term.
        if ( is_wp_error( $term_link ) ) {
            continue;
        }
        // We successfully got a link. Print it out.
        print '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>';
    }
    print '</ul></div>';
    }
    add_shortcode('print-organizations2', 'print_organizations2');

    This outputs the entire list. Even when I replace $firstcolumn with a hard number it still displays the whole list. What am I missing? any thoughts?

Viewing 4 replies - 1 through 4 (of 4 total)
  • When using an offset you also have to make sure that the number you are returning is less than the total otherwise you get the whole list so you have to offset by the count of the first list and explicitly tell it to grab what’s left.

    $terms = get_terms( 'organization', array('offset' => $firstcolumn ));

    becomes

    $terms = get_terms( 'organization', array('offset' => $firstcolumn , 'number' => $secondcolumn));
    Thread Starter mattregister

    (@mattregister)

    Awesome. That fixed it!

    Any idea how one would include an additional column using this same method? I assume the entire math formula in the conditional statement would have to change, but any attempt I’ve made so far hasn’t produced any useable results. Just lots of repeated terms.

    Scratch that. Got a 3rd column working by creating a custom offset and tweaking the formula a bit to reflect 3 columns.

    Thanks for pointing me in the right direction with this method, been trying to come up with a way to do this for a while now.

    function print_organizations3() {
    $countterms = wp_count_terms( 'base_suppliers_category' );
    if($countterms&1) {
    $firstcolumn = ( ( $countterms - 1) / 3 );
    } else {
    $firstcolumn = ( $countterms  / 3 ); }
    
    $secondcolumn = ( ( $countterms - $firstcolumn ) / 2 );
    $thirdcolumn = ( ( $countterms - $firstcolumn ) / 2 );
    $thirdcolumnoffset = ( $firstcolumn + $secondcolumn );
    
    $terms = get_terms( 'base_suppliers_category', array('offset' => $thirdcolumnoffset, 'number' => $thirdcolumn));
    print '<div class="four columns">';
    foreach ( $terms as $term ) {
         // The $term is an object, so we don't need to specify the $taxonomy.
        $term_link = get_term_link( $term );
        // If there was an error, continue to the next term.
        if ( is_wp_error( $term_link ) ) {
            continue;
        }
        // We successfully got a link. Print it out.
        print '<li><a href="#'. $term->slug.'">' . $term->name . '</a></li>';
    }
    print '</div>';
    }
    add_shortcode('print-organizations3', 'print_organizations3');
Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘get_terms offset working?’ is closed to new replies.