从内部循环中获取图像详细信息get_posts()
并删除内部foreach
循环可能会解决您的问题。
注意:替换$info['something_like_post_image']
为您的图像字段。
$post_info = get_posts();
foreach ($post_info as $info) {
//$photos = glob('design/img/*');
//foreach ($photos as $photo) {
echo " <a href='feed.php?pid=" . $info['post_id'] . " ' >
<div style='background:#FFF5C3'> <br> <h2> " . $info['person_mentioned'] . " </h2>
<h3 style='color: black'> " . $info['body'] . " </h3> </div> </a>";
echo " <img src='" . $info['something_like_post_image'] . "' width='285px' height='200px' style='border: 5px solid black'>";
//}
}
更新
/*
* If your images have any naming convention like
* imageFileName = "image_{POST_ID}.jpg"
* then you can use below code (NO DATABASE ENTRY REQUIRED)
* (ie, For post #1 image file would be "image_1.jpg";
* and for post #2 image file would be "image_2.jpg")
*/
$post_info = get_posts();
foreach ($post_info as $info) {
//filename = image_1.jpg or image_2.jpg or...
$photoFileName = 'design/img/' . 'image_' . $info['post_id'] . '.jpg';
if (file_exists($photoFileName)) {
echo " <a href='feed.php?pid=" . $info['post_id'] . " ' >
<div style='background:#FFF5C3'> <br> <h2> " . $info['person_mentioned'] . " </h2>
<h3 style='color: black'> " . $info['body'] . " </h3> </div> </a>";
echo " <img src='" . $photoFileName . "' width='285px' height='200px' style='border: 5px solid black'>";
}
}
注意:您应该根据您的独特形象与每个帖子保持联系;否则,您将无法在发布时通过您的帖子获得该独特图像。查看以下选项以处理这种情况。
- 您可以将图像名称保存在数据库中(对于每个帖子,您可以直接从数据库中获取图像名称)
- 为您的图像使用命名约定(对于帖子 #1,使用唯一的图像名称(例如 image_1,对于帖子 #2 image_2 等)
更新 - 2
如果您正在寻找循环图像(没有任何条件),请使用以下代码
/*
* If you are looking for a solution that cycles each images
* along with each post, try this one
*/
$post_info = get_posts();
$photos = glob('design/img/*');
$numPhotos = count($photos) + 1;
//assuming your post# starts with 1
$imageId = 1;
foreach ($post_info as $info) {
//cycling
if ($imageId % $numPhotos === 0) {
$imageId = 1;
}
$photoFileName = 'design/img/' . 'image_' . $imageId++ . '.jpg';
//no need of this checking, since you are cycling
//if (!file_exists($photoFileName)) {
// $photoFileName = 'path/to/default/image.jpg';
//}
echo " <a href='feed.php?pid=" . $info['post_id'] . " ' >
<div style='background:#FFF5C3'> <br> <h2> " . $info['person_mentioned'] . " </h2>
<h3 style='color: black'> " . $info['body'] . " </h3> </div> </a>";
echo " <img src='" . $photoFileName . "' width='285px' height='200px' style='border: 5px solid black'>";
}