-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.php
68 lines (56 loc) · 2.02 KB
/
plugin.php
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/**
* Plugin Name: Latest Posts Block
* Description: Display and filter latest posts.
* Requires at least: 5.7
* Requires PHP: 7.0
* Version: 0.1.0
* Author: The WordPress Contributors
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: latest-posts
*
* @package blocks-course
*/
/**
* 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/block-editor/tutorials/block-tutorial/writing-your-first-block-type/
*/
function blocks_course_render_latest_posts_block($attributes) {
$args = array(
'posts_per_page' => $attributes['numberOfPosts'],
'posts_status' =>'publish',
'order' => $attributes['order'],
'orderby' => $attributes['orderBy'],
'cat' => $attributes['category']
);
$recent_posts = get_posts($args);
$posts = '<ul ' . get_block_wrapper_attributes() . '>';
foreach($recent_posts as $post) {
$title = get_the_title($post);
$title = $title ? $title : __('(No Title)', 'latest-posts');
$permalink = get_permalink($post);
$excerpt = get_the_excerpt($post);
$posts .= '<li>';
if( $attributes['displayFeaturedImage'] && has_post_thumbnail($post) ) {
$posts .= get_the_post_thumbnail($post, 'large' );
}
$posts .= '<h5><a href="' . esc_url($permalink) . '">' . $title . '</a></h5>';
$posts .= '<time datetime="' . esc_attr( get_the_date('c', $post) ) . '">' . esc_html(get_the_date('', $post)) . '</time>';
if(!empty($excerpt)) {
$posts .= '<p>' . $excerpt . '</p>';
}
$posts .= '</li>';
}
$posts .= '</ul>';
return $posts;
}
function blocks_course_latest_posts_block_init() {
register_block_type_from_metadata( __DIR__ , array(
'render_callback' => 'blocks_course_render_latest_posts_block'
) );
}
add_action( 'init', 'blocks_course_latest_posts_block_init' );