Skip to content

Instantly share code, notes, and snippets.

@zoonderkins
Last active September 2, 2019 13:19
Show Gist options
  • Save zoonderkins/8dab97a6ef9bbddfb6b6d350fc85d46f to your computer and use it in GitHub Desktop.
Save zoonderkins/8dab97a6ef9bbddfb6b6d350fc85d46f to your computer and use it in GitHub Desktop.
wordpress-api-sample

Wordpress api sample (2019-09-02, Wizardamigos Taipei)

index.html

<html>
  <head>
    <meta charset="UTF-8" />
    <title>
      TITLE
    </title>
  </head>
  <body>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <div id="app"></div>
    <script>
      // Javascript goes here
      const app = document.getElementById("app");
      //   app.innerHTML = ` HELLO WORLD `;

      // Axio start here

      axios
        .get(
          "https://cross-http.herokuapp.com/https://okz.moe/wizardamigos/wp-json/wp/v2/posts"
        )
        .then(function(response) {
          //   console.log(response.data[0].title.rendered);
          let title = response.data;
          let result = "";
          let content = "";
          for (let i = 0; i < title.length; i++) {
            // console.log(title[i].title.rendered);
            result += title[i].title.rendered;
            content = title[i].content.rendered;
          }
          //   let title = JSON.stringify(response.data[0].title.rendered);
          //   let title1 = JSON.stringify(response.data[1].title.rendered);
          app.innerHTML = `Title: ${result[0]} <br> Content: ${content} `;
        })
        .catch(function(error) {
          throw error;
        })
        .finally(function() {});
    </script>
  </body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment