2017-07-17 74 views
0

我試圖從我的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>';

回答

0

您應該有$ p-> link或$ p-> guid可用。

你應該可以做print_r($ p);在foreach中,並確切地看到你有什麼數據 - 如果你在這裏複製這個響應以及這可能會有所幫助,這將是有用的。

0

試試這個它會爲你工作:

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

歡迎堆棧溢出,這是一個正確的,因爲你試圖回答,但一個貧窮的答案,使之更好的請提供您在這種情況下,使用鏈接的資源wordpress文檔並解釋你的代碼在做什麼。 –

+0

嘿感謝您的建議 這裏是資源:https://developer.wordpress.org/reference/functions/get_permalink/ –

相關問題