Add "default image" instead of empty image

Post Reply
Gohh
Ssr. Bludit
Posts: 11
Joined: Fri Aug 23, 2019 11:32 pm

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 ?>
User avatar
Edi
Site Admin
Posts: 3121
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Has thanked: 54 times
Been thanked: 77 times
Contact:

Then you have to add an else condition with the path to the default image.
Clickwork - Websites mit Bludit | Planet Bludit - Tipps und Snippets
Gohh
Ssr. Bludit
Posts: 11
Joined: Fri Aug 23, 2019 11:32 pm

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?
GracefulForm
Jr. Bludit
Posts: 3
Joined: Tue Aug 15, 2023 12:39 am
Location: Texas
Has thanked: 3 times

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 } ?>
Last edited by GracefulForm on Wed Aug 16, 2023 3:36 am, edited 1 time in total.
Post Reply