将帖子从一个wordpress站点拉到另一个wordpress站点,并链接到每个帖子

时间:2017-07-17 16:14:49

标签: php json wordpress

我试图使用WP REST API将我的wordpress网站上的帖子拉到另一个网站,我已经成功完成了这一点并且帖子在其他网站上显示得非常好,但现在的问题是我想要每个帖子可点击,以便在点击它时打开完整的文章(帖子)....

$json = file_get_contents('http://mywebsite.com/blog/wp-json/posts?filter[posts_per_page]=4');
// Convert the JSON to an array of posts
$posts = json_decode($json);
foreach ($posts as $p){
    echo '<div style="color: #fff; float: left;" class="col-md-3 col-sm-3 col-lg-3">';
    // Output the featured image (if there is one)
    echo $p->featured_image ? '<img src="' . $p->featured_image->guid . '">' : '';
    echo '<h5>Title: ' . $p->title . '</h5>';
   // echo '<p>Date:  ' . date('F jS', strtotime($p->date)) . '</p>';
    $summary = $p->excerpt;
    $pos=strpos($summary, ' ', 100);
    $summary = substr($summary, 0, 100);
    echo '<p>';
    echo $summary;
    echo '</p>';
    echo '</div>';
}

所以,我现在需要的是将链接拉到每个帖子旁边......

我只想到这样的事情:echo '<p>Link: ' . $p->link. '</p>';

2 个答案:

答案 0 :(得分:0)

你应该有$ p-&gt;链接或$ p-&gt; guid可用。

你应该可以做print_r($ p);在foreach中,如果您在此处复制该响应以及可能有所帮助,那么确切地查看您拥有的数据将会非常有用。

答案 1 :(得分:0)

试试这个会对你有用:

<?php
    $link = get_permalink($p->ID);     // This will get the link of the post from post ID.   
    echo '<p>Link: ' . $link. '</p>';
?>
相关问题