Skip to content

Instantly share code, notes, and snippets.

@renemorozowich
Last active January 17, 2023 22:29
Show Gist options
  • Save renemorozowich/18a79b9f35f7d0f8fdbf5c8f7908c6c6 to your computer and use it in GitHub Desktop.
Save renemorozowich/18a79b9f35f7d0f8fdbf5c8f7908c6c6 to your computer and use it in GitHub Desktop.
Gets the latest two posts from a blog via the REST API. Blog link, title and date included.
<?php
/**
* Plugin Name: Get Posts via REST API
* Description: Gets the latest two posts from a blog via the REST API. Blog link, title and date included.
* Plugin URI: https://renemorozowich.com
* Author: Rene Morozowich
* Version: 1.0
* Text Domain: getpostsviarestapi
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.txt
*
* @package getpostsviarestapi
*/
// Disable direct file access.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Get posts via REST API.
*/
function get_posts_via_rest() {
// Initialize variable.
$allposts = '';
// Enter the name of your blog here followed by /wp-json/wp/v2/posts and add filters like this one that limits the result to 2 posts.
$response = wp_remote_get( 'https://www.sumydesigns.com/wp-json/wp/v2/posts?per_page=2' );
// Exit if error.
if ( is_wp_error( $response ) ) {
return;
}
// Get the body.
$posts = json_decode( wp_remote_retrieve_body( $response ) );
// Exit if nothing is returned.
if ( empty( $posts ) ) {
return;
}
// If there are posts.
if ( ! empty( $posts ) ) {
// For each post.
foreach ( $posts as $post ) {
// Use print_r($post); to get the details of the post and all available fields
// Format the date.
$fordate = date( 'n/j/Y', strtotime( $post->modified ) );
// Show a linked title and post date.
$allposts .= '<a href="' . esc_url( $post->link ) . '" target=\"_blank\">' . esc_html( $post->title->rendered ) . '</a> ' . esc_html( $fordate ) . '<br />';
}
return $allposts;
}
}
// Register as a shortcode to be used on the site.
add_shortcode( 'sc_get_posts_via_rest', 'get_posts_via_rest' );
@caetanodaroda
Copy link

Hello,
after compile and install the plugin, how can I fetch the api content in a specific page? thanks
pi

@renemorozowich
Copy link
Author

Here is the link to the full post: https://renemorozowich.com/using-wordpress-rest-api-get-blogs/

You'd want to put the shortcode there in your screenshot above: [sc_get_posts_via_rest]

@jmrezaee
Copy link

jmrezaee commented Feb 8, 2020

Hello,
Thanks very useful for me.
But I wanna add load more btn, what can I do?

@renemorozowich
Copy link
Author

There are probably a few ways you can do this, but notice on line 29 where it says per_page=2. I would probably change this to the actual number of posts that you want total.

From there, only show a few and hide the rest. You can add a button to show the rest.

@backfolder
Copy link

backfolder commented Mar 6, 2021

Hi! For me Plugin has stop working. I'm actually try to identify problem, if is a CSS recent added or any other thing. : (
Plugin is working perfectly, maybe some script is interfering.
Thanks Rene.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment