drws65968272 2012-01-23 16:21
浏览 92
已采纳

wordpress:如果存在特色图像,则创建条件语句以加载特定图像,否则为默认图像

I've searched for similar questions to mine here, but haven't found a thread that helps me.. although I did find help here to get to the point where I am now ;)

I've created a jquery slideshow that pulls images from the featured images in a certain post category. I found the code below here at SO and got it to work to my needs so far, but I'd need to add a conditional statement to it:

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>

that works ok, but.. i'd like to use something like below to be able to load a default image into the slideshow, if there is no featured image attached to the post:

<?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 }
 ?>

I realise that the below example doesn't work.

  • 写回答

2条回答 默认 最新

  • duanhuokuang5280 2012-01-23 16:44
    关注

    I think the main culprit in your code is src="<?php the_post_thumbnail() ?>"

    as the_post_thumbnail() will echo out the html needed for the entire image tag so try this instead...

    <?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条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算