Display the latest WordPress articles, implemented using Vue
P粉331849987
P粉331849987 2023-08-29 15:45:38
0
1
435
<p>I am rebuilding my WordPress blog using Vue. I'm trying to display my last 3 posts, I did it here: </p> <pre class="brush:php;toolbar:false;"><template> <section> <h4>Recent articles</h4> <div class="posts-recent"> <div v-for="(post, index) in posts.slice(0, 3)" :key="post.id" class="post"> <div :key="index"> <nuxt-link :to="`/blog/${post.slug}`" class="post-image" :style="{ backgroundImage: 'url(' post.fimg_url ')' } "></nuxt-link> <div class="post-text"> <nuxt-link :to="`/blog/${post.slug}`" class="post-title" v-html="post.title.rendered"></nuxt- link> </div> </div> </div> </div> </section> </template> <script> import { mapState, mapActions } from 'vuex'; export default { name: 'Recent', created() { this.getPosts(); }, props: { slug: String }, computed: { ...mapState(['posts']), }, methods: { ...mapActions(['getPosts']), } }; </script></pre> <p>This code works fine, but I want to display 3 articles while excluding the current article if the current article is one of the 3 most recent articles. </p>
P粉331849987
P粉331849987

reply all(1)
P粉070918777

If you have the following article array

[
  { id: 1, slug: 'abc', url: 'https://' },
  { id: 2, slug: 'def', url: 'https://' },
  { id: 3, slug: 'ghi', url: 'https://' },
]

You can use a method similar to this.array.filter(post => post.slug !== this.$route.params.slug) to filter.
If this.$route.params.slug is equal to def, it will only give posts with ids 1 and 3.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!