使用Axios按slug过滤时,WordPress REST API无法正常工作

时间:2018-05-15 17:50:37

标签: wordpress vue.js axios

我能够使用axios和Vue.js显示WordPress帖子内容。一旦我切换到slug过滤,我就无法显示帖子内容。

<template>
  <div>
    <article>
    <h2 class="subtitle">{{ post.title.rendered }}</h2>
    <div v-html="post.excerpt.rendered"></div>
  </article>
  </div>
</template>

<script>
import axios from "axios";
import Router from 'vue-router'

export default {
  name: 'ShowPost',
  data () {
    return {
      post: []
    }
  },
  created() {
           this.slug = this.$route.params.slug;
       },
  mounted() {
           axios({ method: "GET", "url": "https://wpdemo.stevensoehl.com/wp-json/wp/v2/posts?slug=" + this.slug }).then(json => {
               this.post = json.data;
           }, error => {
               console.error(error);
           });
       }
}
</script>

2 个答案:

答案 0 :(得分:0)

有必要检查控制台中是否存在跨域问题,并且可能没有跨域问题

答案 1 :(得分:0)

我想出了一个解决方案。在我关于个人帖子的链接中,我将slug和id作为params

import axios from "axios";

export default {
  name: 'ShowPost',
  data () {
    return {
      post: []
    }
  },
  created() {
           this.id = this.$route.params.id;
       },
  mounted() {
           axios({ method: "GET", "url": "https://wpdemo.stevensoehl.com/wp-json/wp/v2/posts/" + this.id }).then(json => {
               this.post = json.data;
           }, error => {
               console.error(error);
           });
       }
}

路由是slug并按id过滤响应。它现在按计划运作。

<?php
$array_1 = [4,6,2,6];
$array_2 = [ 0=> "DK", 1=>"GA", 2=>"DK", 3=>"GA"];
$newArray = [];
foreach($array_2 as $key=>$value){
  if(isset($newArray[$value])){
    $newArray[$value] +=$array_1[$key];
  }else{
    $newArray[$value] =$array_1[$key];
  }
}
print_r($newArray);
?>