Created
September 17, 2015 22:57
-
-
Save justintadlock/c197797f33cdf5e73750 to your computer and use it in GitHub Desktop.
This file contains 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 | |
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. ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this! Just helped me immensely on a project I'm working on.