How to show WordPress Posts in Laravel application using WordPress API.

Jacques MBABAZI
3 min readSep 20, 2023

--

Let’s suppose you have laravel e-commerce application that need blog and we know that there is no other CMS that can beat WordPress. You also had a blog that contains the article that describes your products. In this case we don’t need to create a new blogging system in our laravel e-commerce application,we need to call WordPress API that will give us all posts into our application.

Consider supporting me by buying me a coffee or subscribe to my buy me a coffee subscription.

Let’s dive into the codes to see what I am talking about,

Set Up Your WordPress Site

Make sure you have a WordPress website with some blog posts that you want to display in your Laravel application. Ensure that your WordPress site has the REST API enabled.

Create a Laravel Project

If you haven’t already, create a new Laravel project using Composer:

creating laravel application

Change into the project directory:

Create a Controller

Create a new controller that will fetch and display the WordPress blog posts:

Laravel controller to fetch posts

If you are using late laravel version you need to install the required packages to make HTTP requests to the WordPress API and to handle JSON data:

composer require guzzlehttp/guzzle

Be sure to replace 'https://your-wordpress-site.com/wp-json/wp/v2/posts' with the actual URL of your WordPress site's API endpoint.

Create a Blade View

Create a Blade view file to display the WordPress posts. You can create a new file in the resources/views/wordpress directory, for example, index.blade.php, and add the following code:

Note you can stlye the way you want the posts to look.

Create a Route:

Open routes/web.php and add a route to your WordpressController:

Test Your Application:

Start the Laravel development server:

Visit http://localhost:8000/wordpress-posts in your web browser to see the WordPress blog posts displayed in your Laravel application. Remember to customize the styles and layout as needed to match the look and feel of your Laravel application. This basic setup allows you to fetch and display WordPress blog posts in your Laravel application using the WordPress REST API.

Again support my work buy by buying me a coffee or subscribe to my buy me a coffee subscription.

Happy Coding!

--

--

Jacques MBABAZI
Jacques MBABAZI

Written by Jacques MBABAZI

Content creator about Laravel and it's ecosystem, Laravel developer with four years of experience. Support me here https://www.buymeacoffee.com/loficoders

No responses yet