UPDATED v3の
まず最初:あなたはinherit
を見ているという記事が 改訂されたか、または添付ファイルが追加されるためです。だからpublish
ステータスの投稿 投稿を選択する必要があります。 3最新のポストを得るために
MySQLのクエリは次のとおりです。
SELECT
posts.ID AS post_id,
posts.post_title,
posts.post_date
FROM
whrg_posts AS posts
WHERE
posts.post_status = 'publish'
AND posts.post_type = 'post'
ORDER BY
posts.post_date DESC
LIMIT 3;
それは
this tutorialに記載されているようにあなたがポストとPostmetaのためのモデルを設定していると仮定します。私たちはまずすべての投稿を取得する必要がありますし、我々はその投稿への添付ファイル
src
を取得する必要があります。
Public function getPosts()
{
return BlogPost::with('postmetas')
->status()
->type()
->orderBy('post_date', 'DESC')
->limit(3)
->get();
}
ブログ投稿のモデルでは、このgetPosts()
方法を交換し、あなたのコントローラにあなたがショーに
public function anyPostList()
{
$postImages = []; //array to hold the featured image URL in a key value pair
$BlogPost = new BlogPost();
$posts = $BlogPost->getPosts();
foreach ($posts as $post)
{
if (!empty($post->postmetas))
{
foreach ($post->postmetas as $postmeta)
{
//if _thumbnail_id exists then get its guid for src
if ($postmeta->meta_key == '_thumbnail_id' && !empty($postmeta->meta_value)){
$thumbnail_id = $postmeta->meta_value;
$attachment = $BlogPost->getPostById($thumbnail_id);
if(!empty($attachment->guid))
$postImages[$post->ID] = $attachment->guid;
}
}
}
}
$data = [
'posts' => $posts,
'postImages' => $postImages
];
return view('test.post', $data);
}
のようにアクセスすることができ、あなたのブログ投稿モデル
に
Public function getPostById($post_id)
{
return BlogPost::where('ID', $post_id)
->first();
}
を、この機能を追加ブレードのポスト:project_name/reso urces /ビュー/テスト/ post.blade.php
@foreach($posts as $post)
<h1>{{ $post->post_title }}</h1>
<span>Published on : {{ $post->post_date }}</span>
@if(isset($postImages[$post->ID]))
<img src="{{$postImages[$post->ID]}}" width="200"/>
@endif
<div>
{{ $post->post_content }}
</div>
@endforeach
はそれが役に立てば幸い!
参考:Laravel docsからAccessing WordPress Post through Laravel
? (wpdomain/wp-json/wp/v2/posts) –
すべての投稿に画像がありますか? –