简体   繁体   English

如何查询我的 WordPress 博客以列出我的 Vue.js 网站上的最新帖子?

[英]How do I query my WordPress blog to list most recent posts on my Vue.js website?

I'm trying to get my most recent posts from my website but am having trouble with calling the API correctly.我正在尝试从我的网站获取我最近的帖子,但无法正确调用 API。 What am I doing wrong?我究竟做错了什么?

I am using WordPress API version 1.1 and Vue.js version 2.0.我使用的是 WordPress API 1.1 版和 Vue.js 2.0 版。 I have Axios properly installed and all the necessary tools including Vue.js Devtools to see the results of a proper API call.我已经正确安装了 Axios 和所有必要的工具,包括 Vue.js Devtools 来查看正确的 API 调用的结果。

But, once I call the API it doesn't list any posts.但是,一旦我调用 API,它就不会列出任何帖子。

<template>
    <div>
        <div>
            <p>{{posts}}</p>
        </div>
    </div>
</template>

<script>
import Axios from "axios"

    export default {
        name: 'Blog',
        mounted() {
            this.getPosts();
        },
        data() {
            return {
                postsURL: "https://public-api.wordpress.com/rest/v1.1/sites/ninjawebsitedesign.com/blog/posts/?number=2&pretty=true",
                posts: [],
                postsData: {
                    per_page: 20,
                    page: 1
                }
            }
        },
        methods: {
            getPosts() {
                Axios.get(this.postsUrl, { params: this.postsData })
                    .then((response) => {
                        this.posts = response.data
                    })
                    .catch((error) => {
                        console.log(error)
                    })
            }
        }
    }
</script>

<style scoped>
    ul li {
        color: black !important;
    }
</style>

您需要在 wordpress 上启用 REST API 并使用此 url api: https : //ninjawebsitedesign.com/wp-json/posts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM