Show more posts in WordPress sidebar (like YouTube)

I am trying to create a YouTube Esque sidebar ‘Show More’ button.

The posts from the same category are displayed in my template sidebar using the following code:

<div class="sidebar-left">
    <div class="grid-container">
            <?php foreach( $posts as $post ):
             setup_postdata( $post );
             $entry_id = $post->ID;
             $entry_categories = get_the_category($entry_id);

            foreach ( $entry_categories as $entry_category ) {
                    $entry_cats = $entry_category->cat_name;
            <a href="<?php the_permalink(); ?>">
                    <div class="grid-item-meta">
                            <span><?php echo $entry_cats; ?></span>
                            <h3><?php the_title(); ?></h3>
                            <h5>Written by <?php echo get_the_author(); ?></h5>
        <?php endforeach; ?>

Is it possible to limit this query / offset it and fetch more on clicking a ‘Show More’ button. Once show more has been toggled I need to change it to ‘Show Less’. I’m quite happy pulling all posts in one query and hiding them on the front end (i’m not too concerned with getting into complex AJAX).

Limiting the height of the div could cut off a post in the middle. Seems there is lots of info / answers on loading more mosts via AJAX or loading a post in a particular div, however not a simple show and hide scenario like I’m after. Many thanks in advance.

Read more here: Show more posts in WordPress sidebar (like YouTube)

Leave a Reply

Your email address will not be published. Required fields are marked *