用于过滤特色图片文章的IF语句

时间:2016-07-11 作者:Jason Jason

我需要的frontpage显示文章,有特色的图像。

如果文章上载了特色图片,请显示它。

如果文章没有上传特色图片,则不显示。

我在网上搜索,特色图片代码是:

<?php if ( \'\' != $thumb ) { ?>
    <div class="single-post-thumbnail">
        <?php echo $thumb; ?>
    </div>
<?php }
如何在下面的php中添加if条件?我曾多次尝试插入它,但都出错了。

<?php 

include locate_template( \'composer/assets/section-colors.php\' );


$ti_featured_posts = new WP_Query(
    array(
        //\'post_type\' => \'post\',
        \'meta_key\' => \'featured_post_add\',
        \'meta_value\' => \'1\',
        \'posts_per_page\' => \'15\',
        \'no_found_rows\' => true,
    )
);
?>

<section class="home-section featured-posts<?php echo $section_bg . \'\' . $section_text. \'\' . $section_links; ?>">

    <div class="wrapper">

        <?php
        /**
         * Section Main & Sub titles
        **/
        $main_title = get_sub_field( \'featured_main_title\' );
        $sub_title = get_sub_field( \'featured_sub_title\' );

        if( $main_title || $sub_title ) : ?>
        <header class="section-header">
            <div class="section-title<?php echo $title_with_sep; ?>">
                <h2 class="title"><?php echo $main_title; ?></h2>
            </div>
            <?php if ( $sub_title ): ?>
            <span class="sub-title"><?php echo $sub_title; ?></span>
            <?php endif; ?>
        </header>
        <?php endif; ?>

        <?php
        if ( $ti_featured_posts->have_posts() ) :

            $posts_column = 0; // Count the posts
            ?>

            <div class="grids entries">


                <?php
                while ( $ti_featured_posts->have_posts() ) : $ti_featured_posts->the_post();


                if ( $posts_column == 0 ) : // Middle column

                    $posts_image_size = \'rectangle-size-big\';
                    $posts_width = \'\';
                    echo \'<div class="grid-6 column-middle">\';

                elseif ( $posts_column == 1 ) : // Right column

                    $posts_width = \'grid-6\';
                    $posts_image_size = \'rectangle-size-small\';
                    echo \'<div class="grid-6 column-right"><div class="grids grid-layout featured-carousel">\';

                endif;
                ?>

                    <article <?php post_class($posts_width); ?>>
                        <div class="post-item-inner">

                            <figure class="entry-image">
                                <a href="<?php the_permalink(); ?>">
                                    <?php 
                                    if ( has_post_thumbnail() ) {
                                        the_post_thumbnail( $posts_image_size );
                                    } elseif( first_post_image() ) { // Set the first image from the editor
                                        echo \'<img src="\' . first_post_image() . \'" class="wp-post-image" />\';
                                    } ?>
                                </a>
                            </figure>

                            <div class="entry-details">

                                <header class="entry-header">
                                    <div class="entry-meta">
                                       <span class="entry-category"><?php the_category(\', \'); ?></span>
                                    </div>

                                    <h2 class="entry-title">
                                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                    </h2>

                                    <?php if ( $posts_column == 0 ) : ?>
                                    <span class="written-by"><?php _e( \'by\',\'themetext\' ); ?></span>
                                    <span class="author">
                                        <a href="<?php echo get_author_posts_url( get_the_author_meta( \'ID\' ) ); ?>" rel="author">
                                            <?php the_author_meta( \'display_name\' ); ?>
                                        </a>
                                    </span>
                                    <?php endif; ?>
                                </header>

                                <?php if ( $posts_column == 0 ) : ?>
                                <div class="entry-summary">
                                    <?php the_excerpt(); ?>
                                </div>
                                <?php endif; ?>

                            </div>

                        </div>        
                    </article>

                <?php
                    if ( $posts_column == 0 ) :

                        echo \'</div><!-- .middle-right -->\'; // Close middle column

                    elseif ( ( $ti_featured_posts->current_post + 1 ) == ( $ti_featured_posts->post_count ) ) : // Close right column

                        echo \'</div><!-- .featured-carousel --></div><!-- .column-right -->\';

                    endif;

                    $posts_column++;

                endwhile;
                ?>

                <?php wp_reset_postdata(); ?>

            </div><!-- .grids -->

            <div class="carousel-navigation"></div>

            <?php else : ?>

                <p class="message">
                    <?php _e( \'There are no featured posts yet\', \'themetext\' ); ?>
                </p>

    <?php endif; ?>

    </div><!-- .wrapper -->
</section><!-- Featured Posts -->
问题已编辑

MichałSkrzypek建议,

<figure class="entry-image">
                            <a href="<?php the_permalink(); ?>">
                                <?php 
                                if ( has_post_thumbnail() ) {
                                    the_post_thumbnail( $posts_image_size );
                                } elseif( first_post_image() ) { // Set the first image from the editor
                                    echo \'<img src="\' . first_post_image() . \'" class="wp-post-image" />\';
                                } ?>
                            </a>
</figure>
更改为

<figure class="entry-image">
            <a href="<?php the_permalink(); ?>">
            <?php
                if( has_post_thumbnail() ) { ?>
                    <figure class="entry-image">
                            <a href=<?php the_permalink(); ?>"><img src="<?php the_post_thumbnail(); ?>" title="<?php the_title(); ?>" /></a>
                 </figure>
            <?php } ?>
            </a>
</figure>
结果是

enter image description here

没有特色图片的文章仍在播放。

标题正在显示。它以前没有出现过。

“title=”I accidentally broke the super-rugged Cat S60 smartphone”/>

1 个回复
SO网友:Michał Skrzypek

如果我理解正确,那么您希望根据是否上传来显示特色图像。如果是,则基本代码为:

<?php
if( has_post_thumbnail() ) { ?>
<a href=<?php the_permalink(); ?>"><img src="<?php the_post_thumbnail(); ?>" title="<?php the_title(); ?>" /></a>
<?php } ?>
并显示链接到帖子的图像。从您提供的代码来看,您很想在其中包含图像:

<figure class="entry-image">
                            <a href="<?php the_permalink(); ?>">
                                <?php 
                                if ( has_post_thumbnail() ) {
                                    the_post_thumbnail( $posts_image_size );
                                } elseif( first_post_image() ) { // Set the first image from the editor
                                    echo \'<img src="\' . first_post_image() . \'" class="wp-post-image" />\';
                                } ?>
                            </a>
</figure>
问题是,在这种形式中,无论是否显示特征图像,您的代码都会显示一些内容,因此您基本上需要将所有代码包装在if语句中,并在中间将其删除,如下所示:

<?php
if( has_post_thumbnail() ) { ?>
 <figure class="entry-image">
      <a href=<?php the_permalink(); ?>"><img src="<?php the_post_thumbnail(); ?>" title="<?php the_title(); ?>" /></a>
 </figure>
<?php } ?>
现在,声明将检查您的帖子是否附有特色图片,然后尝试在全班显示<让我知道你的情况,如果我明白你的意思。

相关推荐