mardi 9 février 2016

How to adapt my code to select a random image from a folder instead of the same default placeholder image using php

I want to change some php code to select a random image from a specified directory rather than the same default image it currently selects. This is the piece of code that currently selects a default fallback image - if there is no image available:

<?php
                                $postCover = '';

                                if ($post->hasImage()) {
                                        $postCover = $post->getImage($photoSize);
                                }

                                if (!$post->hasImage() && $params->get('photo_legacy', true)) {
                                        $postCover = $post->getContentImage();
                                }

                                if (!$postCover && $params->get('show_photo_placeholder', false)) {
                                        $postCover = $post->getImage($photoSize, true);
                                }
                                ?>

                                <?php if ($postCover) { ?>
                                <div class="eb-mod-thumb<?php if ($photoAlignment) { echo " is-" . $photoAlignment; } ?> <?php if (isset($photoLayout->full) && $photoLayout->full) { echo "is-full"; } ?>">
                                        <?php if (isset($photoLayout->crop) && $photoLayout->crop) { ?>
                                    <a href="<?php echo $post->getPermalink();?>" class="eb-mod-image-cover"
                                        style="
                                            background-image: url('<?php echo $postCover;?>');
                                            <?php if (isset($photoLayout->full) && $photoLayout->full) { ?>
                                            width: 100%;
                                            <?php } else { ?>
                                            width: <?php echo $photoLayout->width;?>px;
                                            <?php } ?>
                                            height: <?php echo $photoLayout->height;?>px;"
                                    ></a>
                                        <?php } else { ?>
                                    <a href="<?php echo $post->getPermalink();?>" class="eb-mod-image"
                                        style="
                                            <?php if (isset($photoLayout->full) && $photoLayout->full) { ?>
                                            width: 100%;
                                            <?php } else { ?>
                                            width: <?php echo (isset($photoLayout->width)) ? $photoLayout->width : '260';?>px;
                                            <?php } ?>"
                                    >
                                        <img src="<?php echo $postCover;?>" alt="<?php echo $post->title;?>" />
                                    </a>
                                        <?php } ?>
                                </div>
                                <?php } ?>

I think it is this line I need to change:

" alt="title;?>" />

I have found this solution on here: PHP pull random image from folder

<?php

$dir = "images/";
$images = scandir($dir);
$i = rand(2, sizeof($images)-1);
?> <img src="images/<?php echo $images[$i]; ?>" alt="" />

Which seems to be able to do what I want, but I am unsure how to incorporate it (or where) in the code I have supplied (not having php experience, but trying to learn).

Can anyone help me?

Kind regards

Mel




Aucun commentaire:

Enregistrer un commentaire