我有这部分带有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)。可能是,我的代码有问题所以请纠正我,我的主要目标,正如你在上面的代码中看到的,我只是想获得精选图像并显示其他如果没有特色图像然后显示默认或后备特色图片。
答案 0 :(得分:1)
问题解决了,谢谢合作!
我的解决方案是......
如果是特色图片那么..
$banner = "<img class='extend' src='".$image[0]."' style='width: 100%;' />";
如果找不到特色图片,则设置默认值或后备
$link = get_template_directory_uri();;
$banner = "<img class='extend' src='$link/images/banner.jpg' style='width: 100%;' />";
否则,如果指定类别上没有帖子,则设置默认值或回退
的内容
<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>
表示图片
$link = get_template_directory_uri();;
$banner = "<img class='extend' src='$link/images/banner.jpg' style='width: 100%;' />";