Post placeholders

Ever have a layout set on a fixed number of posts that sometimes stops short? Ever wanted an easy solution for creating placeholder to fill those empty spaces?

Here ya go.

[pastacode lang=”php” message=”Just after you’re query that looks something like this:” highlight=”” provider=”manual”]

$new_query = new WP_Query( 'posts_per_page' => 9, ); // adjust number as needed

[/pastacode]

[pastacode lang=”php” message=”Add the following:” highlight=”” provider=”manual”]

$posts_per_page = $new_query->get( 'posts_per_page' );
$post_count = $new_query->post_count;
$placeholders_needed = $posts_per_page - $post_count;

[/pastacode]

[pastacode lang=”php” message=”Then, after your loop’s endwhile, add the following:” highlight=”” provider=”manual”]

if ( $placeholders_needed > 0 ) : 
    foreach ( range( 1, $placeholders_needed ) as $current_placeholder ) :
	echo 'ADD YOUR PLACEHOLDER CONTENT';
endforeach; endif;

[/pastacode]

Insert the content you want to make the series of posts display seamlessly, the placeholder filling in for however many posts you fall short.

Thnx @Rarst heart

Jenny Beaumont

Jenny Beaumont is an Agile Coach and the Director of Delivery at Human Made, makers of Altis DXP. She speaks at conferences around the world (ok, these days only on zoom), and is a former lead organizer of WordCamp Paris and WordCamp Europe.

Follow me on Twitter | Subscribe to my newsletter