Skip to content

Instantly share code, notes, and snippets.

  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
WordPress page template with API (javascript and php)
<?php /* Template Name: Awesome Page */
<div class="latest-posts"></div>
const url = 'http://localhost:8888/ieatwebsites-v2/wp-json/wp/v2/posts';
const postsContainer = document.querySelector('.latest-posts');
.then(response => response.json())
.then(data => { post => {
const innerContent =
<a href="${}">Read More</a>
postsContainer.innerHTML += innerContent;
$response = wp_remote_get( 'http://localhost:8888/ieatwebsites-v2/wp-json/wp/v2/posts' );
$posts = json_decode( wp_remote_retrieve_body( $response ) );
echo '<div class="latest-posts">';
foreach( $posts as $post ) {
echo '<li><h2>'.$post->title->rendered.'</h2>'.$post->excerpt->rendered.'<a href="' . $post->link . '">Read More</a></li>';
echo '</div>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment