-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmn_recentposts.php
More file actions
102 lines (65 loc) · 2.73 KB
/
mn_recentposts.php
File metadata and controls
102 lines (65 loc) · 2.73 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/*
* Plugin Name: MN Recent Posts
* Plugin URI: https://github.com/mariusgnicula/recent-posts
* Description: A custom recent posts widget with feature image, title, category, excerpt and a read more link.
* Version: 0.2
* Author: Marius Nicula
* Author URI: https://www.linkedin.com/in/mariusgnicula
*/
// recent posts function start
function mn_recent_posts($atts) {
// added the ability to pass in the number of posts, defaults to 4
// added the ability to pass in post_type, defaults to post
$a = shortcode_atts( [
'number' => 4,
'post_type' => 'post'
], $atts );
// parse it to an integer just in case
$custom_number = (int)$a['number'];
$custom_type = $a['post_type'];
// query args
// maybe make it more dynamic in the future
$mn_args = [
'post_type' => $custom_type,
'posts_per_page' => $custom_number
];
// WP_Query declaration
$mn_recent_query = new WP_Query( $mn_args );
// query loop condition
// if posts exist, echo recent posts start tag
if ( $mn_recent_query->have_posts() ) {
// recent posts container start tag
echo '<div class="mn-post__container">';
// loop start
while ( $mn_recent_query->have_posts() ) {
$mn_recent_query->the_post();
// post start tag
echo '<div class="mn-post">';
// article image
// using background-image to maintain a cover aspect
// not the best for SEO, but I'll try and fix it in a later version
$id = get_the_id();
$feature = get_post( get_post_thumbnail_id( $id ) );
$feature_id = $feature->ID;
$feature_link = wp_get_attachment_image_src( $feature_id, 'full' );
$feature_link = $feature_link[0];
echo '<div class="mn-post__feature" style="background-image: url(' . $feature_link . ')">';
echo '<a href="' . get_permalink() . '"></a>';
echo '</div>';
echo '<div class="mn-post__details">';
echo '<a href="' . get_permalink() . '"><h2>' . get_the_title() . '</h2></a>';
echo '<div class="mn-post__category">';
the_category( ', ' );
echo '</div>';
the_excerpt();
echo '<a href="' . get_permalink() . '" class="mn-post__more">Citeste articol</a>';
echo '</div>';
echo '</div>';
}
wp_reset_postdata();
echo '</div>';
}
}
add_shortcode('mn_recent_posts', 'mn_recent_posts');
?>