这就是我加载后期缩略图图像的方式?
如果没有图像,我想显示默认图像怎么办?
我怎样才能证明这一点。
下面是我用来显示缩略图的代码。
$html .= \'<div class="post event" id="post-\'.$post->ID.\'">
<a href="\'.get_permalink($post->ID).\'"> \'.get_the_post_thumbnail().\' </a>
<div class="content">
<span class="title">
<a title="\'.$post->post_title.\'" href="\'.get_permalink($post->ID).\'">\'.$post->post_title.\'</a>
</span>
<div class="addl-123">\';
$terms = wp_get_post_terms($post->ID, \'sublocation\');
if($terms)
{
$out = array();
foreach ($terms as $term)
{
$out[] = \'<a class="\' .$term->slug .\'" href="\' .get_term_link( $term->slug, \'sublocation\') .\'">\' .$term->name .\'</a>\';
}
//echo join( \', \', $out);
$html .= join( \', \', $out);
}
if($is_parent != 0)
{
$address = get_post_meta($is_parent,\'address\',true);
}
else
{
$address = get_post_meta($post->ID,\'address\',true);
}
if(!$address){ $address=\'-\'; }
if($address && $address!="-"){$html .= "<b>".$address."</b>";}
$html .= \'<p>Timinigs: \'.get_post_meta($post->ID, \'cstimings\', true). \' </p>\';
$html .= \'</div>
在代码的第2行,我能够显示具有特色图像的帖子的缩略图。但其他帖子没有特色图片。所以,对于这样的帖子,我想显示默认图像。
我该怎么做?
SO网友:Brad Dalton
此代码可从子主题函数文件中使用。
此外,如果帖子中没有图像,您还可以设置默认回退:
function wpforce_featured() {
global $post;
$already_has_thumb = has_post_thumbnail($post->ID);
if (!$already_has_thumb) {
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment_id);
}
} else {
set_post_thumbnail($post->ID, \'414\');
}
}
} //end function
add_action(\'the_post\', \'wpforce_featured\');
add_action(\'save_post\', \'wpforce_featured\');
add_action(\'draft_to_publish\', \'wpforce_featured\');
add_action(\'new_to_publish\', \'wpforce_featured\');
add_action(\'pending_to_publish\', \'wpforce_featured\');
add_action(\'future_to_publish\', \'wpforce_featured\');
来源:
http://wpforce.com/automatically-set-the-featured-image-in-wordpress/