Page 1 of 1

Add "default image" instead of empty image

Posted: Wed Apr 08, 2020 12:58 pm
by Gohh
Hello, if the image of the post is not selected, how to add "default image"? I print this:

Code: Select all

<?php if ($page->coverImage()) : ?>
    <a href="<?php echo $page->permalink(); ?>">
        <img src="<?php echo $page->coverImage(); ?>">
    </a>
<?php endif ?>

Re: Add "default image" instead of empty image

Posted: Wed Apr 08, 2020 1:38 pm
by Edi
Then you have to add an else condition with the path to the default image.

Re: Add "default image" instead of empty image

Posted: Wed Apr 08, 2020 2:25 pm
by Gohh
Edi wrote: Wed Apr 08, 2020 1:38 pm Then you have to add an else condition with the path to the default image.
Thank you, it worked!

Code: Select all

<?php if (empty($page->coverImage())) : ?>
	<img src="<?php echo '/bl-themes/1/img/default-img.jpg' ?>" alt="">
<?php endif ?>
How can I get a link to the current theme?

Re: Add "default image" instead of empty image

Posted: Tue Aug 15, 2023 12:49 am
by GracefulForm
Using the above messages, here is a complete code, assuming your image is called 'default_img.jpg' and it is stored in your theme's 'img' folder:

Code: Select all

<?php if ($page->coverImage()) { ?>
	<a href="<?php echo $page->permalink(); ?>">
        	<img src="<?php echo $page->coverImage(); ?>">
	</a>
<?php } else { ?>
	<a href="<?php echo $page->permalink(); ?>">
		<img src="<?php echo (HTML_PATH_THEME_IMG . 'default_img.jpg') ?>"
	</a>
<?php } ?>