-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlatest-post-intro.php
More file actions
52 lines (46 loc) · 1.53 KB
/
latest-post-intro.php
File metadata and controls
52 lines (46 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
/**
* Plugin Name: Latest Post Block
* Description: A simple dynamic block used to display introduction for the latest available post.
* Requires at least: 5.8
* Requires PHP: 7.0
* Version: 1.0.0
* Author: gutenberghub
* License: MIT
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: latest-post-intro
*
* @package gutenberghub
*/
/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function gutenberghub_latest_post_intro_block_init() {
register_block_type( __DIR__ . '/build', array(
'render_callback' => function() {
$recent_post = wp_get_recent_posts( array(
'numberposts' => 1,
'post_status' => 'publish',
'post_type' => 'post',
) );
$recent_post = reset( $recent_post );
if ( empty( $recent_post ) ) {
return "";
}
$recent_post_title = $recent_post['post_title'];
$recent_post_excerpt = $recent_post['post_excerpt'];
$recent_post_permalink = get_permalink( $recent_post );
return "
<div class='wp-block-gutenberghub-latest-post-intro'>
<a target='_blank' href='$recent_post_permalink'>$recent_post_title</a>
<p>$recent_post_excerpt</p>
</div>
";
}
) );
}
add_action( 'init', 'gutenberghub_latest_post_intro_block_init' );