Updated answer
更新了dan在这篇帖子上评论的代码。
<?php
$args = array(
\'post_parent\' => $post->ID,
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\',
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
\'offset\' => \'0\',
\'numberposts\' => 1
);
$images = get_posts($args);
if ( count( $images ) > 0 ) {
echo \'<img src="\'. wp_get_attachment_url($images[0]->ID) . \'" alt="Main Image" id="mainImage"" />\';
} else {
echo \'<img src="http://hellotokyostore.com/wp-content/themes/hellotokyo/images/default.png" alt="Hello Tokyo"/>\';
}
?>
<小时>
This information is now out of date but may be useful to another.
我想你想做的是:
每篇文章都有一个缩略图如果没有附加缩略图,请使用默认图像this address. 看看它,这样你就可以了解“特色图片”是如何工作的。
要点是在你的帖子页面上有一个名为“特色图片”的新框,你可以在其中为每篇帖子设置一个“缩略图”。
将以下内容添加到主题functions.php
.
<?php
if (function_exists(\'add_theme_support\')) {
add_theme_support(\'post-thumbnails\');
/* change these dimensions to suit your website.
noticed you used 250 * 250 on the page you linked in
OP comments, so I\'ve used that. you may want to increase
the size of your individual page\'s thumbnails */
add_image_size(\'index-categories\', 250, 250, true);
add_image_size(\'page-single\', 250, 250, true);
}
function InsertFeaturedImage($content) {
global $post;
$original_content = $content;
$default_thumbnail = get_bloginfo(\'template_directory\') . \'/images/default_thumbnail.png\';
$default_src = \'<img src="\' . $default_thumbnail . \'" alt="Your caption here" />\';
if ( current_theme_supports( \'post-thumbnails\' ) ) {
if ( ( is_page() ) || ( is_single() ) ) {
/* if you\'re viewing a single post
250 * 250 as designated above */
if ( has_post_thumbnail() ) {
$content = the_post_thumbnail(\'page-single\');
} else {
$content = $default_src;
}
$content .= $original_content;
} else {
/* if you\'re viewing a list of posts, use this size of thumbnail
250 * 250 as designated above */
if ( has_post_thumbnail() ) {
$content = the_post_thumbnail(\'index-categories\');
} else {
$content = $default_src;
}
$content .= $original_content;
}
}
return $content;
}
add_filter( \'the_content\', \'InsertFeaturedImage\' );
?>
请毫不犹豫地询问这其中是否有任何部分不合理。