【问题标题】:Conditional featured image with youtube thumbnail in WordpressWordpress 中带有 youtube 缩略图的条件特色图片
【发布时间】:2016-07-20 21:44:23
【问题描述】:

基本上我在这里要做的是:显示 youtube 缩略图。如果没有 youtube 缩略图 -> 显示帖子的特色图片。如果没有特色图片 -> 显示后备图片。

但是我似乎做错了什么,因为网页显示为空白。

<?php
            // Check if the post has a Youtube thumbnail (using custom field video_url)
    if get_post_meta($post->ID,'video_url',true)
        echo '<img src="http://img.youtube.com/vi/' . get_post_meta($post->ID,'video_url',true) . '/0.jpg"/>';
            // Check if the post has a Post Thumbnail assigned to it
    else ( has_post_thumbnail() ) {
        echo '<a href="' . get_permalink($post->ID) . '" >';
        the_post_thumbnail('frontpage-thumb');
        echo '</a>';
            // If the post does not have a featured image then display the fallback image
    } else {
        echo '<a href="' . get_permalink($post->ID) . '" ><img src="'. get_stylesheet_directory_uri() . '/img/fallback-featured-image-index.jpg" /></a>';}
    ?>

显示带有自定义字段的 youtube 缩略图的一般代码是 &lt;img src="http://img.youtube.com/vi/&lt;?php echo get_post_meta($post-&gt;ID,'video_url',true);?&gt;/0.jpg"/&gt; 我只是无法让它与条件语句一起使用...

【问题讨论】:

    标签: php wordpress conditional


    【解决方案1】:

    在您的第一个 if 条件中,您的语法错误,必须关闭 PHP

    if (get_post_meta($post->ID,'video_url',true)) // 
    

    正确的代码

    <?php
    
        if (get_post_meta($post->ID,'video_url',true)) ?>
            echo '<img src="http://img.youtube.com/vi/<?php echo get_post_meta($post->ID,'video_url',true);?>/0.jpg"/>';
    
        // Check if the post has a Post Thumbnail assigned to it
        else ( has_post_thumbnail() ) {
            echo '<a href="' . get_permalink($post->ID) . '" >';
            the_post_thumbnail('frontpage-thumb');
            echo '</a>';
    
        // If the post does not have a featured image then display the fallback image
        } else {
            echo '<a href="' . get_permalink($post->ID) . '" ><img src="'. get_stylesheet_directory_uri() . '/img/fallback-featured-image-index.jpg" /></a>';}
    ?>
    

    【讨论】:

    • 现在请检查一下,我确实编辑了我的答案。如果有任何问题,请告诉我
    • 您的代码无效。请从true)) ?&gt; 中删除?&gt; 另外,这不起作用。什么都没有显示。
    猜你喜欢
    • 2011-03-11
    • 1970-01-01
    • 2014-09-28
    • 2012-04-02
    • 2013-07-19
    • 2016-11-21
    • 1970-01-01
    • 2019-03-22
    • 2015-04-05
    相关资源
    最近更新 更多