Last active
October 11, 2018 09:25
-
-
Save websupporter/bb09b39bc2680150275f5cdbc0716f53 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
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
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