Quick overview of the steps we'll cover

Fetch endpoints using WordPress documentation

  1. Retrieve all categories to list them in our navigation.
  2. Fetch a category based on the slug of the current page URL.
  3. Get all posts associated with the category ID of the current page.

Connect data from endpoints

  • List all categories from endpoint 1.
  • Create a single category page and connect its data using API 2.
  • On the single category page, display all posts belonging to the current category using API 3.

Style category link

Ensure the current category link in the navigation has a different style.

REST API video tutorial: A step-by-step guide


How to prepare endpoints from WordPress

First, we need to prepare three endpoints that we need. In the next step, we will use Divhunt REST API application and connect those 3 endpoints that we can use in the builder.

Retrieve all categories

Using the endpoint below, we will fetch up to 30 categories from WordPress. For each category, we will store only the ID, slug, and name.

https://yourwebsite.com/wp-json/wp/v2/categories?per_page=30&_fields=id,slug,name

Retrieve category by slug

Using the endpoint below, we will fetch data for a specific category based on its slug. Later, in Divhunt, we will dynamically provide the slug from the page URL using the REST API.

https://yourwebsite.com/wp-json/wp/v2/categories?slug=CATEGORY_SLUG

Get posts of current category

Using the endpoint below, we will fetch posts that belong to a specific category using its ID. Later, in Divhunt, we will dynamically provide the category ID from the page data using the REST API.

https://yourwebsite.com/wp-json/wp/v2/posts?categories=CATEGORY_ID

Connect WordPress endpoints with Divhunt easily

In the Divhunt REST API application, we will need to create three GET requests.

The first request will retrieve up to 30 categories from WordPress. The second request will fetch data for a specific category based on the page URL slug. The third request will get all posts that belong to the category of the current page.

These requests will allow us to dynamically display categories and their associated posts on our website.

Get all categories

  1. In the URL (Endpoint) field, paste the URL that we generated previously.
  2. Ensure the "Source" option is checked under the settings tab.
  3. Click the execute icon in the middle of the top bar to verify everything is working correctly. If you see a JSON data preview on the right (as shown in the screenshot), everything is correct, and we are ready to use this data in the builder.
  4. Now this data is ready to be used later in builder.
How To Get All Categories From WordPress

Get single category based on a page URL (dynamic slug)

  1. In the URL (Endpoint) field, paste the URL that we generated previously.
  2. Ensure the "Source" option is checked under the settings tab.
  3. Create a dynamic property slug with the default value. (see screenshot 2 below)
  4. Use this property as dynamic slug in our endpoint instead of static slug '${properties->get('slug')}'. (screenshot 1 below)
  5. Click the execute icon in the middle of the top bar to verify everything is working correctly. If you see a JSON data preview on the right (as shown in the screenshot), everything is correct, and we are ready to use this data in the builder.
  6. Now this data is ready to be used later in builder.
How To Get A Single Category From WordPress
Advanced Settings - Properties Preview

Get posts of current category

  1. In the URL (Endpoint) field, paste the URL that we generated previously.
  2. Ensure the "Source" option is checked under the settings tab.
  3. Create a dynamic property categoryid with the default value. (see screenshot 2 below)
  4. Use this property as dynamic category id in our endpoint instead of static category id '${properties->get('categoryid')}'. (see screenshot 1 below)
  5. Click the execute icon in the middle of the top bar to verify everything is working correctly. If you see a JSON data preview on the right (as shown in the screenshot), everything is correct, and we are ready to use this data in the builder.
  6. Now this data is ready to be used later in builder.
How To Get Posts Of Current Category
Advanced Settings - Properties Preview

Now all this data is being stored under the Source of your website, and you can use it to display information on your website. Click here to learn more about Source.