Last active
February 18, 2023 05:52
-
-
Save uddhabh/e64c92d2dee14aacea4c7a07e3662292 to your computer and use it in GitHub Desktop.
This PHP code defines a shortcode function that retrieves the latest posts from a WordPress site's API and displays them in a specific format on a webpage. It uses a transient to store the latest posts for a certain time period to improve performance. The shortcode can be added to a WordPress page or post using the add_shortcode function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function latest_posts_shortcode() { | |
// Set the transient key and expiration time | |
$transient_key = 'latest_posts'; | |
$expiration = DAY_IN_SECONDS; | |
// Define the site URL and number of posts per page | |
$site_url = 'https://example.com'; | |
$posts_per_page = 3; | |
// Get the latest posts from the transient or the API | |
if (false === ($posts = get_transient($transient_key))) { | |
// Retrieve the latest posts from the API | |
$api_url = $site_url . '/wp-json/wp/v2/posts?per_page=' . $posts_per_page; | |
$data = file_get_contents($api_url); | |
$posts = json_decode($data); | |
// Set the transient with the latest posts and expiration time | |
set_transient($transient_key, $posts, $expiration); | |
} | |
// Initialize output variable | |
$output = ''; | |
// Loop through the posts and append them to the output | |
$output .= '<div class="latest-posts-wrapper">'; | |
$count = 0; | |
foreach($posts as $post) { | |
$title = $post->title->rendered; | |
$link = $post->link; | |
$featured_image = $post->featured_media; | |
$date = date("F d, Y", strtotime($post->date)); | |
$excerpt = $post->excerpt->rendered; | |
$excerpt = substr($excerpt, 0, 100) . '...'; | |
// Retrieve the URL for the featured image | |
$image_data = file_get_contents($site_url . '/wp-json/wp/v2/media/' . $featured_image); | |
$image = json_decode($image_data); | |
$image_url = $image->media_details->sizes->medium->source_url; | |
$output .= '<div class="latest-post">'; | |
$output .= '<a href="' . $link . '" target="_blank" rel="noopener">'; | |
$output .= '<img src="' . $image_url . '" alt="' . $title . '">'; | |
$output .= '</a>'; | |
$output .= '<div class="latest-post-details">'; | |
$output .= '<h2><a href="' . $link . '" target="_blank" rel="noopener">' . $title . '</a></h2>'; | |
$output .= '<p class="date">' . $date . '</p>'; | |
$output .= '<div class="excerpt">' . $excerpt . '</div>'; | |
$output .= '</div>'; | |
$output .= '</div>'; | |
$count++; | |
if ($count % 3 == 0) { | |
$output .= '</div><div class="posts-row">'; | |
} | |
} | |
$output .= '</div>'; | |
return $output; | |
} | |
add_shortcode( 'latest_posts', 'latest_posts_shortcode' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment