GCBGutenberg
Control Blocks
v1.3.3
Buy

Post grid

Cards for your latest posts. Your client chooses how many, which category and how many columns. Every file shown in full.

A grid of cards for your latest posts: the featured image, the date, the title and a short excerpt. Your client chooses how many posts, which category and how many columns. A new post appears in the grid when it is published. It is three plain files in your theme and a stylesheet.

Download example A zip of one block folder for your theme. Needs GCB 1.3.1 or later, and nothing else.

The whole block

Create blocks/post-grid/ in your theme. The folder name is the block's name, so this registers as gcb/post-grid.

block.json is WordPress's standard block metadata. There are no attributes in it and no render key: GCB makes the attributes from the fields and finds render.php by itself.

blocks/post-grid/block.json
{	"apiVersion": 3,	"name": "gcb/post-grid",	"title": "Post grid",	"description": "Cards for your latest posts. Your client chooses how many, from which category and in how many columns.",	"category": "widgets",	"icon": "grid-view",	"supports": { "html": false, "align": [ "wide", "full" ] },	"style": "file:./style.css"}

block.fields.json lists four fields. How many is a number with a validation rule, so a save with fewer than 1 or more than 12 is refused. Category is a taxonomy field locked to WordPress's own categories, with "multiple": false so it takes one. It stores { taxonomy, ids }. Columns is a range: a slider from 2 to 4 that stores a number.

blocks/post-grid/block.fields.json
{	"controls": [		{ "id": "c_heading", "type": "text", "label": "Heading", "attributeKey": "heading", "default": "Latest posts",			"validation": { "maxLength": 60 } },		{ "id": "c_count", "type": "number", "label": "How many posts", "attributeKey": "count", "default": 3, "min": 1, "max": 12,			"validation": { "min": 1, "max": 12 } },		{ "id": "c_category", "type": "taxonomy", "label": "Category", "attributeKey": "category", "taxonomy": "category", "multiple": false,			"helpText": "Leave empty to show posts from every category." },		{ "id": "c_columns", "type": "range", "label": "Columns", "attributeKey": "columns", "default": 3, "min": 2, "max": 4 }	]}

render.php is the template, for the editor and the page alike. It reads the values from $attributes. An element with data-gcb-field="<attributeKey>" and data-gcb-field-type="<type>" is edited where it sits on the page. A field with no marker appears in the block's sidebar.

Only the heading has a marker here. The three settings are in the sidebar. The posts come from an ordinary get_posts() query, the same as in any theme template. The template still holds the count between 1 and 12 and the columns between 2 and 4 itself, so the page is right whatever was saved. Titles and excerpts are escaped, and the whole card is one link.

blocks/post-grid/render.php
<?php/** * Post grid: a heading and a card for each of the latest posts. * * The heading is typed on the page. How many posts, which category and how many columns are settings in the * sidebar. The posts come from an ordinary WordPress query, so a new post appears here when it is published. * * @var array $attributes The block's field values. */ // A field that stores an object (a taxonomy choice) can arrive as an object or an array.$as_array = static function ( $value ) {	return is_object( $value ) ? json_decode( wp_json_encode( $value ), true ) : (array) $value;}; $heading  = (string) ( $attributes['heading'] ?? '' );$count    = max( 1, min( 12, (int) ( $attributes['count'] ?? 3 ) ) );$columns  = max( 2, min( 4, (int) ( $attributes['columns'] ?? 3 ) ) );$category = $as_array( $attributes['category'] ?? array() );$term_ids = array_filter( array_map( 'absint', (array) ( $category['ids'] ?? array() ) ) ); $query = array(	'post_type'           => 'post',	'post_status'         => 'publish',	'posts_per_page'      => $count,	'ignore_sticky_posts' => true,	'no_found_rows'       => true,);if ( $term_ids ) {	$query['category__in'] = $term_ids;}// On a single post, leave that post out of its own grid.if ( is_singular( 'post' ) ) {	$query['post__not_in'] = array( get_queried_object_id() );}$posts = get_posts( $query );?><section <?php echo get_block_wrapper_attributes( array( 'class' => 'post-grid', 'style' => '--post-grid-columns:' . $columns ) ); ?>>	<?php if ( '' !== $heading ) : ?>		<h2 class="post-grid__heading" data-gcb-field="heading" data-gcb-field-type="text"><?php echo esc_html( $heading ); ?></h2>	<?php endif; ?> 	<?php if ( $posts ) : ?>		<ul class="post-grid__list" role="list">			<?php foreach ( $posts as $item ) : ?>				<li class="post-grid__card">					<?php if ( has_post_thumbnail( $item ) ) : ?>						<div class="post-grid__picture">							<?php echo get_the_post_thumbnail( $item, 'medium_large', array( 'alt' => '', 'loading' => 'lazy' ) ); ?>						</div>					<?php endif; ?>					<div class="post-grid__words">						<time class="post-grid__date" datetime="<?php echo esc_attr( get_the_date( 'c', $item ) ); ?>"><?php echo esc_html( get_the_date( '', $item ) ); ?></time>						<h3 class="post-grid__title"><a href="<?php echo esc_url( get_permalink( $item ) ); ?>"><?php echo esc_html( get_the_title( $item ) ); ?></a></h3>						<p class="post-grid__excerpt"><?php echo esc_html( wp_trim_words( get_the_excerpt( $item ), 24 ) ); ?></p>					</div>				</li>			<?php endforeach; ?>		</ul>	<?php else : ?>		<p class="post-grid__none"><?php esc_html_e( 'No posts to show yet.', 'default' ); ?></p>	<?php endif; ?></section>

style.css is a plain starting look, loaded by "style": "file:./style.css" in block.json. The columns setting arrives as a CSS variable. The grid drops to two columns on a tablet and one on a phone. Replace it with your design.

blocks/post-grid/style.css
.post-grid__heading { margin: 0 0 28px; }.post-grid__list { list-style: none; margin: 0; padding: 0; display: grid; gap: 28px; grid-template-columns: repeat(var(--post-grid-columns, 3), minmax(0, 1fr)); }.post-grid__card { position: relative; display: flex; flex-direction: column; margin: 0; border: 1px solid color-mix(in srgb, currentColor 18%, transparent); border-radius: 14px; overflow: hidden; }.post-grid__picture { display: block; aspect-ratio: 16 / 10; background: #ece8e1; }.post-grid__picture img { width: 100%; height: 100%; max-width: none; object-fit: cover; display: block; }.post-grid__words { padding: 20px 22px 24px; }.post-grid__date { display: block; margin: 0 0 8px; font-size: 13px; letter-spacing: .08em; text-transform: uppercase; opacity: .7; }.post-grid__title { margin: 0 0 8px; font-size: 1.2em; line-height: 1.3; }.post-grid__title a { color: inherit; text-decoration: none; }.post-grid__title a::after { content: ""; position: absolute; inset: 0; }.post-grid__title a:hover { text-decoration: underline; }.post-grid__card:focus-within { outline: 2px solid currentColor; outline-offset: 3px; }.post-grid__excerpt { margin: 0; line-height: 1.55; opacity: .8; }.post-grid__none { margin: 0; opacity: .75; }@media (max-width: 900px) { .post-grid__list { grid-template-columns: repeat(2, minmax(0, 1fr)); } }@media (max-width: 600px) { .post-grid__list { grid-template-columns: 1fr; } }

That is all of it. There is no edit.js, no npm and no build step: save the files, reload the editor, and the block is in the inserter.

What your client does

  • Clicks the heading and types.
  • Sets how many posts to show, from 1 to 12.
  • Picks a category, or leaves it empty for posts from every category.
  • Sets the columns, from 2 to 4.

They don't add the cards: writing a post is enough. A post with no featured image gets a card with no picture.

When you need pages or filters

This block shows the newest few posts and stops. For a list a visitor pages through or filters, use a query-loop field, which runs the query, the pager and the filters for you. To let your client hand-pick the posts, see the collection helper.

Use it

  1. Unzip the download.
  2. Copy post-grid/ into your theme's blocks/ folder.
  3. Reload the block editor. Post grid is in the inserter.

That's all of it. You don't register the block: there's no register_block_type() call and nothing to add to functions.php. GCB registers every folder in your theme's blocks/ folder by itself.

More examples: all examples, or download them together as the GCB Kit. To build one from nothing, see how to build a block.

Build anything in the block editor

Every licence includes all features and a year of updates and support.

Buy GCB from $8.25 a month See pricing