开发者

Wordpress query post get Image unless blank

开发者 https://www.devze.com 2022-12-14 04:29 出处:网络
Hi I am creating a wordpress custom page theme, I have included the code. Is there a way to dynamically add the list items. In the code I grab each image related to its post with screenshot1 screensho

Hi I am creating a wordpress custom page theme, I have included the code. Is there a way to dynamically add the list items. In the code I grab each image related to its post with screenshot1 screenshot2 so on... This all currently works. My problem is at the current moment if I upload 2 screen shots 3 list items will show up and the third will just be blank. So how could I dynamically add them based on the number of images in the post?

query_开发者_开发问答posts('cat=3'); /*--Query to grab Projects categorie--*/

while (have_posts()) : the_post();  /* --- loop through the posts in that categorie */

<ul>

<?php
$screen1 = get_post_meta($post->ID, 'screenshot1', true);
$screen2 = get_post_meta($post->ID, 'screenshot2', true);
$screen3 = get_post_meta($post->ID, 'screenshot3', true);
echo "<li>" . wp_get_attachment_image($screen1, 'large') . "</li>"; 
echo  "<li>" . wp_get_attachment_image($screen2, 'large') . "</li>";
echo  "<li>" . wp_get_attachment_image($screen3, 'large') . "</li>";
?>

</ul>


Loop through the possible meta values and only output it if it's not empty.

Something like:

<ul>
<?php
$i = 1;
while ($screen = get_post_meta($post->ID, 'screenshot'.$i , true)) {
    echo "<li>" . wp_get_attachment_image($screen, 'large') . "</li>"
    $i++; 
}
?>
</ul>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号