• Resolved janet4now

    (@janet4now)


    I have this archive page, that now we want to only show 6 of the categories on the page. I have tried a few different things, but none of them changed anything. Where should I add something to limit the number shown to 6?

    <?php ?>
    <div class="series_archive">
      <hr>
      <h2>Series Archive:</h2>
      <div class="row">
        <?php
        $categories = get_categories( array(
          'parent' => 40 //sermons category
    
        ) );
    
        //$currCatTitle = single_cat_title("", false);
        $currCat = get_queried_object()->cat_ID;
        foreach ( $categories as $category ) {
          if ( $currCat != $category->cat_ID ) { //don't show current category again				
    
            echo sprintf( '<div class="col-sm-4"><a href="%s">%s</a></div>',
              esc_url( get_category_link( $category->term_id ) ),
              do_shortcode( sprintf( '[wp_custom_image_category term_id="%s"]', $category->term_id ) )
            );
          }
        }
        ?>
      </div>
      <div style="clear:both;margin-bottom:24px;"></div>
      <span class="pastSermons"><a href="https://vimeo.com/gracechico" target="_blank">Archived Sermons</a></span> </div>
    </div>

    `

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • I found this :

    
    $categories = get_terms( 'category', array(
        'hide_empty' => 0,
        'offset' => 6,
        'orderby' => 'id',
        'order' => 'DESC'
    ));
    
    Thread Starter janet4now

    (@janet4now)

    Where does that go in my code above? so that my parent category still works, etc.

    Moderator bcworkz

    (@bcworkz)

    You can pass the same arguments to get_categories() that can be passed to get_terms(). I suggest your get_categories() call be modified like so:

    $categories = get_categories( array(
          'parent' => 40, //sermons category
          'number' => 6,
          'exclude' => get_queried_object_id(),
    ) );

    If the current category were in the returned list, you’d only get 5 categories output. It’s better to exclude the current category by passing its ID with the “exclude” argument. By doing so, you don’t need to exlude the current category elsewhere in your code.

    Thread Starter janet4now

    (@janet4now)

    THAT WORKED!!! THANK YOU!!

    • This reply was modified 4 years, 9 months ago by janet4now.
    Thread Starter janet4now

    (@janet4now)

    I’m now trying to order by ID from most recent but it doesn’t seem to work. It pulls only six now, but the order is jumbled, not most recent on down. Anything I need to add here?

    $categories = get_categories( array(
         'parent' => 40, //sermons category
         'number' => 6,
          'orderby' => 'ID',
          'order' => 'DESC',
         'exclude' => get_queried_object_id(),
    ) );
    Thread Starter janet4now

    (@janet4now)

    Argh! I had a plugin that was creating some kind of custom order so I disabled and now everything works perfect!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Limit categories to 6 on the page’ is closed to new replies.