Skip to content

Instantly share code, notes, and snippets.

@devuri
Last active July 4, 2023 17:04
Show Gist options
  • Save devuri/65ff608508c1127c95f03d37fcbc2242 to your computer and use it in GitHub Desktop.
Save devuri/65ff608508c1127c95f03d37fcbc2242 to your computer and use it in GitHub Desktop.
list WordPress post based on categories and limit number of returns
<?php
/*
Plugin Name: Most Recent Posts
Description: Lists posts based on categories and limits the number of returns
*/
function most_recent_posts_shortcode($atts) {
$a = shortcode_atts(
array(
'postnumber' => 3,
'category' => 0,
),
$atts
);
ob_start();
$devuri_recent_posts = wp_get_recent_posts(array(
'numberposts' => $a['postnumber'],
'category' => $a['category'],
'post_status' => 'publish',
));
foreach ($devuri_recent_posts as $devuri_recent) {
echo '<div class="mrp-wrapper" style="padding: 2px;">';
echo '<ul class="mrp-ul" style="padding: 8px; overflow: hidden; border-top: solid thin beige;"><li>';
echo '<div class="mrp-featimage" style="padding-right: 12px;">';
echo '<a href="' . get_permalink($devuri_recent['ID']) . '"> ' . get_the_post_thumbnail($devuri_recent['ID'], 'medium') . '</a>';
echo '</div>';
echo ' <strong><a href="' . get_permalink($devuri_recent['ID']) . '"> ' . $devuri_recent['post_title'] . '</a></strong>';
echo '<br/>' . date('jS F, Y', strtotime($devuri_recent['post_date']));
echo '</li></ul>';
echo '</div>';
}
wp_reset_query();
$output_mrp_obj = ob_get_contents();
ob_end_clean();
return $output_mrp_obj;
}
add_shortcode('most_recent_post', 'most_recent_posts_shortcode');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment