我在这里搜索了类似的问题,但是没有找到一个可以帮助我的线程..虽然我确实在这里找到帮助以达到我现在所处的位置;)
我创建了一个jquery幻灯片,可以从特定帖子类别的特色图像中提取图像。我在这里找到了下面的代码,并且到目前为止它已经满足了我的需求,但是我需要为它添加一个条件语句:
if(has_post_thumbnail()) {
$showImg = wp_get_attachment_image_src( get_post_thumbnail_id ( $post->ID ), 'xiao-show-img' );
} ?>
<a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="<?php the_excerpt() ?>">
<img src="<?php echo $showImg[0]; ?>" title="<?php the_title() ?>" rel="<?php the_excerpt() ?>" />
</a>
工作正常但是..我想使用下面的内容,以便能够将默认图像加载到幻灯片中,如果帖子上没有附加特色图片:
<?php
if(has_post_thumbnail()) { ?>
<a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="<?php the_excerpt() ?>">
<img src="<?php the_post_thumbnail() ?>" title="<?php the_title() ?>" rel="<?php the_excerpt() ?>" />
</a>
<?php
} else {
?>
<a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="<?php the_excerpt() ?>">
<img src="<?php get_bloginfo("template_url"); ?>/images/xiao-default-image.png" />;
</a>
<?php }
?>
我意识到以下示例不起作用。
答案 0 :(得分:3)
我认为代码中的主要罪魁祸首是src="<?php the_post_thumbnail() ?>"
as_post_thumbnail()会回显出整个图片代码所需的html,所以请尝试这样做......
<?php if(has_post_thumbnail()): ?>
<a href="<?php the_permalink() ?>" title="<?php the_title() ?>">
<?php the_post_thumbnail() ?>
</a>
<?php else: ?>
<a href="<?php the_permalink() ?>" title="<?php the_title() ?>">
<img src="<?php get_bloginfo("template_url"); ?>/images/xiao-default-image.png" />;
</a>
<?php endif ?>
答案 1 :(得分:2)
更简单的方法 - 只需将该图片的样式更改为包含background-image: url('images/xiao=default-image.png');
并正确设置width:
和height:
。然后,您不需要if/else
语句,也可以更改$showImg
以包含src
,并为您的图片添加自定义类:
if(has_post_thumbnail()) {
$showImg = 'src="' . wp_get_attachment_image_src( get_post_thumbnail_id ( $post->ID ), 'xiao-show-img' ) . '"';
} ?>
<a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="<?php the_excerpt() ?>">
<img class="usedefault" <?php echo $showImg[0]; ?> title="<?php the_title() ?>" rel="<?php the_excerpt() ?>" />
</a>
你的风格会像......
.usedefault {
background-image: etc...
}