如果在Single.php上存在滑块,请删除特色图片

时间:2014-02-14 18:38:53

标签: php wordpress image featured

以下代码适用于我的single.php。 php if(has_posts())调用标准的wordpress特征图像,function_exists('dfi_get_featured_images')部分抓取以nivo-slider样式包装的滑块图像。

php if(have_posts())循环破坏了脚本。我尝试将其包含在if else语句中,如果帖子中没有幻灯片图片,则只显示wordpress特色图片($ featuredImages!= NULL)。

编码的任何帮助都会很棒。

谢谢......李

<div id="featured" class="row-fluid"> 
<?php 
if ( function_exists('dfi_get_featured_images') ) {            
   $featuredImages = dfi_get_featured_images();             
if( !is_null($featuredImages) ){
 echo "<div class='slider-wrapper theme-default'>";
       echo "<div class='entry-thumbnail nivoSlider' style='width: 1170px;'>";
       foreach($featuredImages as $images) {
           echo "<a href='" . get_permalink() . "' title = '" .     dfi_get_image_alt_by_id($images['attachment_id']) . "'>";
           echo "<img src = '" . $images['thumb'] . "' />";
           echo "</a>";                                        
       }
       echo "</div>";
       echo "</div>";
   }
 } 
if($featuredImages!=NULL)
{

}
else
{
<?php if ( have_posts() ) { ?>
            <?php while ( have_posts() ) { ?>
                <?php the_post(); ?>
                <?php if ( '' != get_the_post_thumbnail() ) { ?>
                    <?php the_post_thumbnail( 'full' ); ?>
                <?php } // end if ?>
            <?php } // end while ?>
        <?php } // end have_posts ?>

  }

?>

1 个答案:

答案 0 :(得分:0)

<?php 
    if ( have_posts() ) { 
        while ( have_posts() ) {
            the_post();

            if ( function_exists('dfi_get_featured_images') ) { // If the featured images function exists         
                $featuredImages = dfi_get_featured_images();    // Get those featured slider images         
                if( !is_null($featuredImages) ) { //If there are featured slider images to get, then:

                    echo "<div class='slider-wrapper theme-default'>";
                    echo "<div class='entry-thumbnail nivoSlider' style='width: 1170px;'>";
                    foreach($featuredImages as $images) {
                        echo "<a href='" . get_permalink() . "' title = '" . dfi_get_image_alt_by_id($images['attachment_id']) . "'>";
                        echo "<img src = '" . $images['thumb'] . "' />";
                        echo "</a>";                                        
                    }
                    echo "</div>";
                    echo "</div>";

                } else {
                    the_post_thumbnail( 'full' );
                }
            } else if ( has_post_thumbnail() ) { //If there are no featured images to get, but there is a thumbnail
                the_post_thumbnail( 'full' );
            } // end else if
        } // end while
    } // end hav_posts          

?>  

有几点需要注意:

您不必在每行的开头和结尾处继续打开和关闭php。它使代码变得更加难以阅读。

想想你真正想做的事情。如果有滑块,你不想删除特色图像 - 你想要显示滑块而不是特色图像(至少,这听起来像是对我来说)。以下是此代码背后的思考过程:

if (has function check if dfi images is registered) {
    check and see if there are andy dfi images
    if there are dfi images {
        loop through them and display them
    } else if dfi images is registered but there are no dfi images {
        display the thumbnail
    }
} else, dfi images must not be registered {
    display the thumbnail
}

因此,您首先检查插件,然后检查插件图像的存在,如果其中任何一个返回否定,您将显示缩略图。