Skip to content

Instantly share code, notes, and snippets.

@fadingdust
Created March 16, 2018 22:34
Show Gist options
  • Save fadingdust/80c546cb783bc4778a7ee7b98988c569 to your computer and use it in GitHub Desktop.
Save fadingdust/80c546cb783bc4778a7ee7b98988c569 to your computer and use it in GitHub Desktop.
Page and Post Router for Vue.js from @gilbitron
<?php
function rest_theme_scripts() {
wp_enqueue_script( 'rest-theme-vue', get_template_directory_uri() . '/rest-theme/dist/build.js', array(), '1.0.0', true );
wp_localize_script( 'rest-theme-vue', 'wp', array(
'routes' => rest_theme_routes(), //Drop in to JS from PHP
) );
}
add_action( 'wp_enqueue_scripts', 'rest_theme_scripts' );
function rest_theme_routes() {
$routes = array();
$query = new WP_Query( array(
'post_type' => 'any',
'post_status' => 'publish',
'posts_per_page' => -1, ) );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$routes[] = array(
'id' => get_the_ID(),
'type' => get_post_type(),
'slug' => basename( get_permalink() ),
);
}
}
wp_reset_postdata();
return $routes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment