How to display WordPress posts on external PHP website


2

I have a custom coded website in Codeigniter (PHP framework). It is located in the root folder. Recently, I've also added a WordPress blog on my website and placed it inside a sub-folder called /blog.

So, basically, the current folder structure of my website looks like this:-

  • application
  • blog <=== This folder contains my WordPress blog
  • system
  • index.php

Now, I want to select 8 latest posts from my WordPress blog and display them on the home page of my Codeigniter based website.

Is it possible? If so, can you please share the code with me?


Share
asked 26 Feb 2022 12:06:52 PM
junaidakhtar

No comment found


Answers

2

It is absolutely possible. And you know what, it only takes a few lines of code to retrieve blog posts from WordPress and display them on any PHP based website.

In this answer, I'll try to explain everything in a bit more detail so that you or any one else looking for a similar solution can find it helpful.

Basically, every latest WordPress is packed with a REST API. It enables us to easily communicate with our WordPress blog from any external application (in your case, it is Codeigniter).

The URL of WordPress REST API looks like this.

https://www.example.com/blog/wp-json/wp/v2/

Note:- Don't forget to replace www.example.com with your domain name.

According to your requirements, you have to use the Posts endpoint of WordPress REST API.

So now the URL will look something like this.

https://www.example.com/blog/wp-json/wp/v2/posts

By default, the posts endpoint will return 10 latest blog posts. But, as you mentioned that you only want to display 8 posts, so you have to pass a query string called per_page and set its value to 8.

Like this:-

https://www.example.com/blog/wp-json/wp/v2/posts?per_page=8

If you also want to get the featured image of blog posts then you have to pass an additional query string parameter called _embed.

Now, our final URL will look like this.

https://www.example.com/blog/wp-json/wp/v2/posts?per_page=8&_embed

Now it's time to use this URL in our PHP code to retrieve the 8 latest blog posts from WordPress and then display them on an external PHP website. In your case, simply copy the below code and paste it inside the Codeigniter's view file of "Home Page". Don't forget to replace the domain name!

Here's the complete code snippet.

<div>
<?php
$blog_posts = json_decode(file_get_contents('https://www.example.com/blog/wp-json/wp/v2/posts?per_page=8&_embed'));

if(empty($blog_posts))
{
?>
    <div>No blog post available</div>
<?php
}
else
{
    foreach($blog_posts as $blog_post)
    {
?>
        <div>
            <a href="<?php echo $blog_post->link; ?>">
                <div>
                    <img src="<?php echo $blog_post->_embedded->{'wp:featuredmedia'}['0']->source_url ?>" alt="<?php echo html_escape($blog_post->title->rendered); ?>" />
                </div>

                <div><?php echo html_escape($blog_post->title->rendered); ?></div>
            </a>
        </div>
<?php
    }
}
?>
</div>

That's it.

If you want to know more about available endpoints of WordPress REST API then visit the below mentioned reference page.

link: WordPress REST API Developer Endpoint Reference


Share
furqan - Profile picture
answered 26 Feb 2022 02:16:22 PM
furqan

Perfect!!! Thanks a lot. — junaidakhtar 26 Feb 2022 02:27:55 PM
You're welcome. — furqan 26 Feb 2022 02:30:22 PM


You must log in or sign up to answer this question.