Display the latest WordPress articles, implemented using Vue
P粉331849987
2023-08-29 15:45:38
<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>
If you have the following article array
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 todef
, it will only give posts with ids1
and3
.