首页  >  问答  >  正文

展示最新的WordPress文章,使用Vue实现

<p>我正在使用Vue重建我的WordPress博客。我正在尝试显示我的最近的3篇文章,我在这里进行了操作:</p> <pre class="brush:php;toolbar:false;"><template> <section> <h4>最近的文章</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>这个代码可以正常工作,但是我希望在显示3篇文章的同时,排除当前的文章,如果当前的文章是最近的3篇文章之一。</p>
P粉331849987P粉331849987416 天前446

全部回复(1)我来回复

  • P粉070918777

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

    如果你有以下的文章数组

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

    你可以使用类似 this.array.filter(post => post.slug !== this.$route.params.slug) 的方法进行 过滤
    如果 this.$route.params.slug 等于 def,它将只给出 id 为 13 的文章。

    回复
    0
  • 取消回复