下面是代码的相关部分:
// This will make a URL like http://yoursite.com/path/to/fallback.png
$fallback_image = site_url( \'/path/to/fallback.png\' );
$fallback_image = "<img src=\'{$fallback_image}\' />";
foreach( $recent_posts as $recent ){
echo \'<div class="sidebar-entries">\';
$featured_image = get_the_post_thumbnail( $recent[\'ID\'], \'sidebar-thumb\', array( \'class\' => \'sidebar-image\' ) );
if ( ! strlen( $featured_image ) ) {
$featured_image = $fallback_image;
}
$permalink = \'<a href="\' . get_permalink( $recent[\'ID\'] ) . \'">%s</a>\';
echo sprintf( $permalink, $featured_image );
echo \'<div class="sidebar-entries-title">\';
echo sprintf( $permalink, __( $recent["post_title"] ) );
echo \'<div class="sidebar-date">\';
$timestamp = \'<p>\' . human_time_diff( strtotime( $recent[\'post_date\'] ), current_time(\'timestamp\') ) . \' ago </p>\';
echo sprintf( $permalink, $timestamp );
echo \'</div>\';
echo \'</div>\';
echo \'</div>\';
}
基本思想是
将回退映像存储在循环外部的变量中,如果得到空字符串(! strlen()
)<将特色图像变量的内容替换为回退图像变量EDIT: 抱歉,忘记了permalink部分。此方法使用sprintf
使permalink代码保持一致。