私は数日間これを達成しようとしています。各投稿には、対応する画像を置き換えたい場所にプレースホルダーを手動で配置した説明があります。例えば:
This is the description shortened...
[image]
[image]
Description starts again with a new paragraph continuing...
プレースホルダーは [画像] です。新しい投稿ごとに複数の画像をアップロードしますが、各投稿は 1 ~ 10 個の画像と異なる場合があるため、配置される [image] プレースホルダーの量はさまざまです。その投稿に関連するすべての画像を取得し、画像の数をカウントする機能があります。
ここに私がこれまでに持っているコードがありますが、最初の 2 つのプレースホルダー [image] に対して最初の関連画像を 2 回表示し、次にループして説明を再度表示します。今回は両方の [image] プレースホルダーを 2 番目の画像に置き換えます。 .
<?php
foreach ($photos as $picture) {
$count = count($picture);
for($i = 1; $i<= $count; $i++) {
$image = $picture['filename'];
$replace = "[image]";
$image_path = '../../content_management/image_upload/';
$placeholders = array("$replace");
$image_location = array('<a class="fancybox" href="' . $image_path . '' . $image . '" data-fancybox-group="gallery"><img src="' . $image_path . '' . $image . '" /></a>');
$rawstring = $photo_article['description'];
$new_string = $rawstring;
$new_string = str_replace($placeholders, $image_location, $new_string, $i);
echo $new_string;
}
}
?>
そして、出力は次のとおりです。
This is the description shortened...
Image1.jpg
Image1.jpg
Description starts again with a new paragraph continuing...
This is the description shortened...
Image2.jpg
Image2.jpg
Description starts again with a new paragraph continuing...