Skip to content

Instantly share code, notes, and snippets.

@websupporter
Last active October 11, 2018 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save websupporter/bb09b39bc2680150275f5cdbc0716f53 to your computer and use it in GitHub Desktop.
Save websupporter/bb09b39bc2680150275f5cdbc0716f53 to your computer and use it in GitHub Desktop.
add_action('init',
function() {
unregister_block_type('core/latest-posts');
register_block_type(
'core/latest-posts',
array(
'attributes' => array(
'categories' => array(
'type' => 'string',
),
'className' => array(
'type' => 'string',
),
'postsToShow' => array(
'type' => 'number',
'default' => 5,
),
'displayPostDate' => array(
'type' => 'boolean',
'default' => false,
),
'postLayout' => array(
'type' => 'string',
'default' => 'list',
),
'columns' => array(
'type' => 'number',
'default' => 3,
),
'align' => array(
'type' => 'string',
),
'order' => array(
'type' => 'string',
'default' => 'desc',
),
'orderBy' => array(
'type' => 'string',
'default' => 'date',
),
),
'render_callback' => 'my_render_block_core_latest_posts',
)
);
},
11
);
function my_render_block_core_latest_posts($attributes) {
$recent_posts = wp_get_recent_posts(
array(
'numberposts' => $attributes['postsToShow'],
'post_status' => 'publish',
'order' => $attributes['order'],
'orderby' => $attributes['orderBy'],
'category' => $attributes['categories'],
)
);
$list_items_markup = '';
foreach ( $recent_posts as $post ) {
$post_id = $post['ID'];
$title = get_the_title( $post_id );
if ( ! $title ) {
$title = __( '(Untitled)', 'gutenberg' );
}
$image = get_the_post_thumbnail( $post_id );
$list_items_markup .= sprintf(
'<li><a href="%1$s">%2$s %3$s</a>',
esc_url( get_permalink( $post_id ) ),
$image,
esc_html( $title )
);
if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
$list_items_markup .= sprintf(
'<time datetime="%1$s" class="wp-block-latest-posts__post-date">%2$s</time>',
esc_attr( get_the_date( 'c', $post_id ) ),
esc_html( get_the_date( '', $post_id ) )
);
}
$list_items_markup .= "</li>\n";
}
$class = 'wp-block-latest-posts';
if ( isset( $attributes['align'] ) ) {
$class .= ' align' . $attributes['align'];
}
if ( isset( $attributes['postLayout'] ) && 'grid' === $attributes['postLayout'] ) {
$class .= ' is-grid';
}
if ( isset( $attributes['columns'] ) && 'grid' === $attributes['postLayout'] ) {
$class .= ' columns-' . $attributes['columns'];
}
if ( isset( $attributes['className'] ) ) {
$class .= ' ' . $attributes['className'];
}
$block_content = sprintf(
'<ul class="%1$s">%2$s</ul>',
esc_attr( $class ),
$list_items_markup
);
return $block_content;
}
@websupporter
Copy link
Author

Currently (pre WP 5.0) there seems not to be a filter to filter the output of the latest posts block. This is probably something which will come soon. Meanwhile, this is a workaround to help with the following request: https://twitter.com/deadfishli/status/1050308721597972480

@websupporter
Copy link
Author

Its basically a copy paste of the original block declaration, you can find here: https://github.com/WordPress/gutenberg/blob/master/packages/block-library/src/latest-posts/index.php

Keep an eye one what changes to this block though.

Disclaimer: I don't know if you would consider this "best practice", but it should work :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment