最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - fallback image for featured image

programmeradmin0浏览0评论

I want to use a fallback image if no featured image is set. I'm using the following code, but the image is not shown...

<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
    <?php the_post_thumbnail( array(334, 259) ); // Declare pixel size you need inside the array ?>
<?php else : // No thumbnail? Showing default is better UX than no image. ?>
    <img src="/wp/wp-content/themes/klicknet-theme/images/testbild.png" 
alt="testbild" width="334" height="259" title="Bild: <?php the_title(); ?>"> 
<?php endif; ?>

Any ideas why?

I want to use a fallback image if no featured image is set. I'm using the following code, but the image is not shown...

<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
    <?php the_post_thumbnail( array(334, 259) ); // Declare pixel size you need inside the array ?>
<?php else : // No thumbnail? Showing default is better UX than no image. ?>
    <img src="/wp/wp-content/themes/klicknet-theme/images/testbild.png" 
alt="testbild" width="334" height="259" title="Bild: <?php the_title(); ?>"> 
<?php endif; ?>

Any ideas why?

Share Improve this question edited Jul 3, 2018 at 18:28 Krzysiek Dróżdż 25.5k9 gold badges53 silver badges74 bronze badges asked Jul 3, 2018 at 14:09 tom84tom84 2551 gold badge6 silver badges21 bronze badges 4
  • It's probably something simple like the wrong URL to the image. First set your else statement to something basic like echo 'No featured image found'; so you can tell whether "else" is ever being met. If it is, then the URL to your image is wrong - unless you have your whole site installed in a "wp" folder, you probably just need to remove /wp from the very beginning of the img src. – WebElaine Commented Jul 3, 2018 at 15:17
  • no i checked it... – tom84 Commented Jul 4, 2018 at 14:26
  • Where are you running this code? Are you sure you're in the Loop so has_post_thumbnail() knows which Post you're asking about? – WebElaine Commented Jul 5, 2018 at 13:51
  • Regarding debugging the image url - first, go to your.domain + the path in src, to check if browser will load the image in the first place. Otherwise I recommend using either get_template_directory_uri() or get_stylesheet_directory_uri() then you don't have to worry where your themefolder is :) developer.wordpress/reference/functions/… , developer.wordpress/reference/functions/… e.g. src="<?php echo get_template_directory_uri(); ?>/images/testbild.png" in your case – jave.web Commented Feb 9, 2021 at 2:05
Add a comment  | 

2 Answers 2

Reset to default 1

Your code looks OK and it should work just fine. But there are some things you can (and you should) fix.

1. You don't use absolute URL for your fallback image

You pass /wp/wp-content/themes/klicknet-theme/images/testbild.png as src of your image. It would be much better and more secure if you'd use WP functions in there. For example like so:

<img src="<?php bloginfo('template_url'); ?>/images/testbild.png">

2. You don't escape the title properly

In your fallback image you use the_title() in title attribute. But you don't escape it as an attribute. If the title contains " character, it will break your HTML. Another problem is that the title can contain HTML tags, and they will be printed in your attribute.

If you want to use title as attribute, you should use the_title_attribute function instead. So the fixed version of that line can look something like this:

<img src="<?php bloginfo('template_url'); ?>/images/testbild.png" alt="testbild" width="334" height="259" title="<?php the_title_attribute( array( 'before' => 'Bild: ', 'after' => '' ) ); ?>"> 

You could add a filter to provide custom <img> html when there's a thumbnail missing.

Note, that although you don't have to anymore this still allows you to use has_post_thumbnail() if necessary, because has_post_thumbnail() is based on get_post_thumbnail_id(), meaning it checks if there really was any image set to that post.

functions.php

// fallback thumbnail
// note, that this still allows you to check has_post_thumbnail() !
// (since it correctly checks if ID is set, not the actual output :))
add_filter('post_thumbnail_html', function ($html) {
  if (! empty($html)) return $html; // FILTER ! MUST RETURN!
  return '<img src="' . get_stylesheet_directory_uri() . '/images/fallback.png' . '">';
}, 999, 1);

Just to be clear, this code assumes you have an images directory in your theme with a fallback.png insside it :) !
If you are expecting child themes, but the fallback image should come from your parent theme, then replace get_stylesheet_directory_uri() with get_template_directory_uri()

Any template code:

<?php the_post_thumbnail( ... ); ?>
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>