简体   繁体   English

使用WP REST API WordPress插件将帖子添加到我的PHP页面

[英]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. 我正在重做我的个人网站,并希望为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. 这样我可以在主索引页面上有3个帖子,我可以用手机更新,而无需编写新内容。我安装了WordPress插件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. 我甚至使用domainname / wp-json / wp / v2 / posts /检查了它,它显示了我创建的四个测试帖子。

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. 我对JSON API一无所知,但我正在努力将这些最近的帖子收集到我的精选帖子部分。 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/ . 首先,您需要从example.com/wp-json/wp/v2/posts/获取WP的帖子。 For that, you need to do a curl GET request. 为此,您需要执行curl GET请求。

Take a look at this tutorial , replace the example domain with your site when making request in your PHP page. 看一下本教程 ,在PHP页面中发出请求时,将示例域替换为您的站点。

The result will be a JSON object. 结果将是一个JSON对象。 Now, do a json_decode() on the result and you should have an array or object. 现在,对结果执行json_decode() ,您应该有一个数组或对象。 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. 我使用了http://www.bmcsquincy.com/featured_posts/wp-json/wp/v2/posts/ ,这就是我知道该部分正在运行的方式。

for the php i used 对于我使用的PHP

    <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-->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Posts and Pages插件中的Allow PHP在页面上进行Wordpress查询 - Wordpress Query on a Page using Allow PHP in Posts and Pages Plugin 使用WordPress插件编码标准和Php Codesniffer验证我的插件时出现“posts_per_page”= -1时出错 - Error with “posts_per_page” = -1 when verify my plugin with WordPress Plugin Coding Standards And Php Codesniffer [posts_per_page] =-1中的WP REST API V2问题 - WP REST API V2 Issue In [posts_per_page]=-1 WordPress:使用页面模板在wp页面中获取帖子 - WordPress : using page template to fetch posts in wp page 我如何使用WordPress REST API从我的角度应用程序中提取更多WordPress帖子 - How can I pull in more WordPress posts from my angular app, using the WordPress REST api 如何通过REST-API获取WordPress中插件的自定义帖子? - How to get custom posts of plugin in WordPress by REST-API? 使用 WP Query 在 wordpress 中以每页 5 个帖子的分页显示最新的 100 个帖子 - Display latest 100 posts with pagination of 5 posts per page in wordpress using WP Query 使用 Wp-Posts 和 Wp-Terms 查询 Wordpress 帖子 - Query Wordpress Posts Using Wp-Posts and Wp-Terms 如何在WordPress插件PHP中编辑mySQL wp_posts表? - How to edit mySQL wp_posts table from plugin php in WordPress? 如何在不是我的Wordpress插件中添加新页面? - How to add new page in not my Wordpress plugin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM