0

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

php if ( have_posts() ) 循环正在破坏脚本。我试图将它包含在 if else 语句中,以便仅在帖子中没有幻灯片图像时才显示 wordpress 特色图像 if($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 ?>

  }

?>
4

1 回答 1

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
}

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

于 2014-02-14T20:45:45.307 回答