变量不离开如果声明

时间:2013-01-22 04:25:59

标签: php wordpress

我正在尝试在Wordpress中为图库显示当前所选的精选图像 - 如果没有选择精选图像,我希望它选择图库中的第一个图像并使用该图像。我写了以下内容,它只返回else语句中的图像。换句话说,如果我选择了特色图像,则不会显示任何图像。如果我告诉php在if语句中回显$ image_img_tag的内容,它将返回图像。但由于某种原因,它不会在if / else语句之外显示该图像。我假设$ image_img_tag的值没有使它超出if / else语句,但无法弄清楚原因。有什么想法吗?

<?php
    if ( has_post_thumbnail() ) :
        $featuredID = get_post_thumbnail_id();
        $image_img_tag = wp_get_attachment_image( $featuredID, 'featured-image');
    else :
        $images = get_children( array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) );
        if ( $images ) :
            $total_images = count( $images );
            $image = array_shift( $images );
            $image_img_tag = wp_get_attachment_image( $image->ID, 'featured-image');
    endif;
?>

<figure class="alignright gallery-thumb">
    <a href="<?php the_permalink(); ?>"><?php echo $image_img_tag; ?></a>
</figure><!-- .alignright .gallery-thumb -->

以下是此区域的完整代码,包括所有ifs / endif。这可能有助于解释问题。

<?php if ( is_search() ) : // Only display Excerpts for search pages ?>
<div class="entry-summary">
    <?php the_excerpt(); ?>
</div><!-- .entry-summary -->
<?php else : ?>
<div class="entry-content">
    <?php if ( post_password_required() ) : ?>
        <?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'matthewbarker' ) ); ?>

    <?php else : ?>
        <?php
            if ( has_post_thumbnail() ) :
                //the_post_thumbnail('featured-image',array('class'=>'alignright'));
                $featuredID = get_post_thumbnail_id();
                $image_img_tag = wp_get_attachment_image( $featuredID, 'featured-image');
            else :
                $images = get_children( array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) );
                if ( $images ) :
                    $total_images = count( $images );
                    $image = array_shift( $images );
                    $image_img_tag = wp_get_attachment_image( $image->ID, 'featured-image');
            endif;
            ?>

        <figure class="alignright gallery-thumb">
            <a href="<?php the_permalink(); ?>"><?php echo $image_img_tag; ?></a>
        </figure><!-- .alignright .gallery-thumb -->

        <p><em><?php printf( _n( 'This gallery contains <a %1$s>%2$s photo</a>.', 'This gallery contains <a %1$s>%2$s photos</a>.', $total_images, 'matthewbarker' ),
                'href="' . esc_url( get_permalink() ) . '" title="' . sprintf( esc_attr__( 'Permalink to %s', 'matthewbarker' ), the_title_attribute( 'echo=0' ) ) . '" rel="bookmark"',
                number_format_i18n( $total_images )
            ); ?></em></p>
    <?php endif; ?>
    <?php the_excerpt(); ?>
<?php endif; ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'matthewbarker' ) . '</span>', 'after' => '</div>' ) ); ?>

1 个答案:

答案 0 :(得分:0)

您可能错过了endif

if ( has_post_thumbnail() ) :
    $featuredID = get_post_thumbnail_id();
    $image_img_tag = wp_get_attachment_image( $featuredID, 'featured-image');
else :
    $images = get_children( array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) );
    if ( $images ) :
        $total_images = count( $images );
        $image = array_shift( $images );
        $image_img_tag = wp_get_attachment_image( $image->ID, 'featured-image');
    endif; #MISSING? 
endif;