Skip to content

Instantly share code, notes, and snippets.

@justintadlock
Created September 17, 2015 22:57
Show Gist options
  • Save justintadlock/c197797f33cdf5e73750 to your computer and use it in GitHub Desktop.
Save justintadlock/c197797f33cdf5e73750 to your computer and use it in GitHub Desktop.
<?php
add_action( 'wp_enqueue_scripts', 'jt_enqueue_scripts' );
function jt_enqueue_scripts() {
wp_enqueue_script( 'wp-api' );
wp_enqueue_script( 'wp-util' );
}
add_action( 'wp_footer', 'jt_print_post_template', 25 );
function jt_print_post_template() { ?>
<script type="text/html" id="tmpl-jt-post">
<article class="post">
<header class="entry-header">
<h1 class="entry-title">
<a href="{{ data.link }}">{{{ data.title.rendered }}}</a>
</h1>
<span class="entry-author">{{ data.date }}</span>
</header>
<div class="entry-content">
{{{ data.excerpt.rendered }}}
</div>
</article>
</script>
<?php }
add_action( 'wp_footer', 'jt_print_scripts', 25 );
function jt_print_scripts() { ?>
<script type="text/javascript">
jQuery( document ).ready( function() {
var post_template = wp.template( 'jt-post' );
var posts = new wp.api.collections.Posts();
posts.fetch().done( function() {
posts.each( function( post ) {
// console.log( post.attributes );
jQuery( '.site-content' ).append( post_template( post.attributes ) );
} );
} );
} );
</script>
<?php }
get_header(); // Loads the header.php template. ?>
<div class="site-content"></div>
<?php get_footer(); // Loads the footer.php template. ?>
@taupecat
Copy link

Thank you for this! Just helped me immensely on a project I'm working on.

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