简体   繁体   中英

Using WP REST API WordPress plugin to add posts to my PHP page

I am redoing my personal website and wanted to add a section for recent posts from wordpress. This way I can have 3 posts on my main index page that I can just update with phone without having to code new things in. I installed the WordPress plugin WP REST API. I even checked it by using the domainname/wp-json/wp/v2/posts/ and it is showing the four test posts I have created.

I don't really know anything about JSON API but I am having the hardest time trying to acquire those recent posts into my featured posts section. I have been scavenging the internet in hopes of a tutorial that would help me but nothing is really showing the post on my page. Anybody have any suggestions?

Here are general pointers based on your question:

First, you need to get the posts from WP from example.com/wp-json/wp/v2/posts/ . For that, you need to do a curl GET request.

Take a look at this tutorial , replace the example domain with your site when making request in your PHP page.

The result will be a JSON object. Now, do a json_decode() on the result and you should have an array or object. You can display the results by iterating over it.

Here's an example displaying all the headlines:

    <section id="blog">
        <div class="container-fluid">
            <div class="row">
                FEATURED POSTS
                <?php
                    // Get cURL resource
                    $curl = curl_init();
                    // Set some options - we are passing in a useragent too here
                    curl_setopt_array($curl, array(
                        CURLOPT_RETURNTRANSFER => 1,
                        CURLOPT_URL => 'http://www.bmcsquincy.com/featured_posts/wp-json/wp/v2/posts/',
                        CURLOPT_USERAGENT => 'Codular Sample cURL Request',
                    ));
                    // Send the request & save response to $resp
                    $resp = curl_exec($curl);
                    // Close request to clear up some resources
                    curl_close($curl);

                    $resp=json_decode($resp, TRUE);
                    //var_dump($resp);

                    foreach($resp as $post) {
                        echo '<h2>' . $post['title']['rendered'] . '</h2><br />';
                    }
                ?>
            </div><!--END ROW-->
        </div><!--END CONTAINER FLUID-->
</section><!--END SECTION BLOG-->

I used http://www.bmcsquincy.com/featured_posts/wp-json/wp/v2/posts/ and this is how i know that part is working.

for the php i used

    <section id="blog">
        <div class="container-fluid">
            <div class="row">
                FEATURED POSTS
                <?php
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://www.bmcsquincy.com/featured_posts/wp-json/wp/v2/posts/',
    CURLOPT_USERAGENT => 'Codular Sample cURL Request',
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => array(
        item1 => 'value',
        item2 => 'value2'
    )
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
?>
            </div><!--END ROW-->
        </div><!--END CONTAINER FLUID-->
</section><!--END SECTION BLOG-->

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM