Created
March 16, 2018 22:34
-
-
Save fadingdust/80c546cb783bc4778a7ee7b98988c569 to your computer and use it in GitHub Desktop.
Page and Post Router for Vue.js from @gilbitron
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 | |
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