【发布时间】:2013-07-13 03:34:55
【问题描述】:
我有这部分带有php代码的html
<div id="featuredtext" class="extend">
<?php
// select a category and the number of post
$featucat = "featured";
$featucount = "1";
?>
<?php
//declare query
$my_query = new WP_Query('showposts='. $featucount .'&category_name='. $featucat .'');
//if there is post then
if ($my_query->have_posts()){
?>
<?php
//start the looping of post
while ($my_query->have_posts()) : $my_query->the_post();$do_not_duplicate = $post->ID; ?>
<?php
//get the featured image of a post
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<?php
//if theres featured image then..
if (has_post_thumbnail()) {
//put the image into a variable
$banner = "<img class='extend' src='".$image[0]."' style='width: 100%;' />";
//else if there is none then..
}else{
//since there is no featured image into the post then let set a fallback..
$banner = "<img class='extend' src='".bloginfo('template_directory')."'/images/banner.jpg' style='width: 100%;' />";
}?>
<h2>
<?php
//display the title
the_title();
?>
</h2>
<p>
<?php
//display the content
the_content();
?>
</p>
<?php
//close the while loop
endwhile; }
//if there is no post on the specified category then display the default or the fallback
else{ ?>
<h2>Yes! This is the <b style="color: #e1e315; font-weight: bold;">Featured text!</b> here you put all shout you had</h2>
<p>a molestie <b style="color: #7e7d7d; font-weight: bold;">lacus hendrerit</b>. In arcu dolor, ulla <b style="color: #7e7d7d; font-weight: bold;" >mcorper sed</b> sollicitudin</p>
<?php } ?>
</div>
<div class="clear extend" style="height: 10px;"></div>
<!-- display the featured image based on the $banner variable -->
<a href="<?php bloginfo('url'); ?>"><?php echo $banner; ?></a>
从上面的代码可以看出,它显示了来自指定类别帖子的内容(标题、内容、特色图片)。显示标题和内容,但不显示特色图片(发布图片/thumbmail)。可能是,我的代码有问题,所以请纠正我,我的主要目标,正如您在上面的代码中看到的那样,我只想获取特色图像并显示它,如果没有特色图像则显示默认值或后备特色图片。
【问题讨论】:
-
您是否在
functions.php中添加了add_theme_support( 'post-thumbnails' );没有任何挂钩? -
featured image的文件夹层次结构与此源文件相关吗? -
问题解决了!无论如何谢谢你的合作:)