EvanAgee / vuejs-wordpress-theme-starter

A WordPress theme with the guts ripped out and replaced with Vue.
https://vuewp.com/
1.6k stars 281 forks source link

How to display single post? #2

Closed sangdth closed 6 years ago

sangdth commented 6 years ago

Hi Evan,

I'm trying to display single post, I've created a SinglePost.vue, add object into /router/index.js

{
    // How to dynamic detect the path?
    path: '/:slug',
    component: SinglePost
}

Right now I'm cheating, he he, change the permalink into "postname" only, to make it easy.

My question is, in WP we have many ways to display the path, from date /2017/10/18/hello-world, to category like /cagegory/hello-world, how could we get it and put it into the path programmatically?

Thanks.

EvanAgee commented 6 years ago

Hey @sangdth here's how I would do it.

For each post you're going to get the slug as part of the post object. For your link to that post you'll want to do a <router-link :to="'/posts/'+post.slug"></router-link>

Then in your router you would do:

{
    // How to dynamic detect the path?
    path: '/posts/:slug',
    component: SinglePost
}

One of the costs of replacing the WordPress theme with a Vue app is that we lose access to some of those handy WordPress functions, unless of course we leverage those to modify the JSON response from the API.

sangdth commented 6 years ago

Thanks, it works.

EvanAgee commented 6 years ago

Awesome, feel free to open a PR if you think your changes might be beneficial to the project!