Skip to content

Instantly share code, notes, and snippets.

@Archie22is
Created May 16, 2024 13:34
Show Gist options
  • Save Archie22is/780cbf31f979f581cdfd6851d0821fb7 to your computer and use it in GitHub Desktop.
Save Archie22is/780cbf31f979f581cdfd6851d0821fb7 to your computer and use it in GitHub Desktop.
Working example of a WordPress Loop Shortcode
<?php
/**
*
*
*/
if (!function_exists('footer_get_latest_post')) {
function footer_get_latest_post() {
ob_start();
$footerPost = '';
$args = array('posts_per_page' => 1 );
$recent_posts = new WP_Query($args);
while( $recent_posts->have_posts() ) {
$recent_posts->the_post();
$footerPost = '<div class="footer-post-thumb">' . the_post_thumbnail('thumbnail') . '</div>';
$footerPost .= '<div class="footer-post-content"><h3>' . the_title() . '</h3>';
$footerPost .= the_content() . '</div>';
}
wp_reset_query();
//return $footerPost;
$output = ob_get_contents();
ob_end_clean();
return $output;
}
}
add_shortcode('FooterLatestPost', 'footer_get_latest_post');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment