Home  >  Q&A  >  body text

Display the latest WordPress articles, implemented using Vue

<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粉331849987P粉331849987416 days ago451

reply all(1)I'll reply

  • P粉070918777

    P粉0709187772023-08-30 15:11:00

    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.

    reply
    0
  • Cancelreply