Complete the code to fetch posts using the WordPress REST API.
fetch('[1]/wp-json/wp/v2/posts') .then(response => response.json()) .then(data => console.log(data));
The WordPress REST API endpoint starts with the site URL followed by /wp-json/wp/v2/posts to fetch posts.
Complete the code to display the title of the first post from the REST API response.
fetch('https://example.com/wp-json/wp/v2/posts') .then(response => response.json()) .then(posts => console.log(posts[[1]].title.rendered));
Arrays in JavaScript start at index 0, so the first post is at posts[0].
Fix the error in the code to correctly fetch and log the post titles.
fetch('https://example.com/wp-json/wp/v2/posts') .then(response => response.[1]()) .then(posts => posts.forEach(post => console.log(post.title.rendered)));
response.text() which returns raw text, not parsed JSON.The response must be converted to JSON using response.json() to access the data as JavaScript objects.
Fill both blanks to create a React component that fetches and displays post titles from the WordPress REST API.
import React, { useState, useEffect } from 'react'; function Posts() { const [posts, setPosts] = useState([]); useEffect(() => { fetch('[1]/wp-json/wp/v2/posts') .then(response => response.[2]()) .then(data => setPosts(data)); }, []); return ( <ul> {posts.map(post => <li key={post.id}>{post.title.rendered}</li>)} </ul> ); }
text() instead of json().The component fetches posts from the WordPress REST API URL and parses the response as JSON before setting state.
Fill all three blanks to create a JavaScript function that fetches posts, filters by category ID 5, and logs the titles.
async function fetchCategoryPosts() {
const response = await fetch('[1]/wp-json/wp/v2/posts?categories=[2]');
const posts = await response.[3]();
posts.forEach(post => console.log(post.title.rendered));
}The function fetches posts from the WordPress REST API filtered by category ID 5 and parses the response as JSON.