如果wordpress中没有精选图片,是否可以删除div?

时间:2014-02-11 00:12:20

标签: wordpress image featured

有没有办法告诉wordpress内容页面如果有特色图片显示内容div并删除div如果没有?我代码Html和CSS,但PHP对我来说是一个模糊。怜悯:)我目前有一个虚拟缩略图代替特色图像,如果它不存在。见下文。谢谢!

<header class="entry-header">

    <?php if ( is_search() || is_archive () || is_category () || is_tag () || is_home() ) : // Only display Excerpts for Search ?>



        <?php if ( is_single() ) : ?>

        <h1 class="entry-title  post-title"><?php the_title(); ?></h1>
        <?php else : ?>

        <?php endif; // is_single() ?>
        <?php if ( comments_open() ) : ?>
            <!--<div class="comments-link">
                <?php // comments_popup_link( '<span class="leave-reply">' . __( 'Leave a reply', 'twentytwelve' ) . '</span>', __( '1 Reply', 'twentytwelve' ), __( '% Replies', 'twentytwelve' ) ); ?>
            </div>--><!-- .comments-link -->
        <?php endif; // comments_open() ?>
        <footer class="entry-meta post-content">

                    <?php

                    if ( has_post_thumbnail() ) {
                        echo '<figure class="cat-thumb">';
                                the_post_thumbnail('category-thumb');
                                echo '</figure>';
                    }
                    else {

                        echo '


                    <figure class="placeholder-thumb">
                        <p><h1 class="entry-title"><?php echo single_post_title() ?></h1></p>
                    </figure>


                    ';


                    }
                    ?>



                <div id="blogPostContent">

                <p class="post-title"><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'twentytwelve' ), the_title_attribute( 'echo=0' ) ) ); ?>"    rel="bookmark"><?php the_title(); ?></a></p>

                <div class="entry-summary">


                <div class="postByLine">
                        <?php 
                    $date = sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s">%4$s</time></a>',
                    esc_url( get_permalink() ),
                    esc_attr( get_the_time() ),
                    esc_attr( get_the_date( 'c' ) ),
                    esc_html( get_the_date() )
                    );

                    $author = sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></span>',
                            esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
                            esc_attr( sprintf( __( 'View all posts by %s', 'twentytwelve' ), get_the_author() ) ),
                            get_the_author()
                        );

                        echo ' Published on ' . $date . ' | By ' . $author . ' | ';


                    if ( comments_open() ) : ?>

                    <?php comments_popup_link( '<span class="leave-reply">' . __( 'Leave a reply', 'twentytwelve' ) . '</span>', __( '1 Reply', 'twentytwelve' ), __( '% Replies', 'twentytwelve' ) ); ?>

                        <?php endif; // comments_open()
                    ?>
                </div>


                    <?php the_excerpt(); ?><span class="read_more"><a href="<?php the_permalink(); ?>">Read More</a></span>
                </div><!-- .entry-summary -->
                <?php else : ?>
                <div class="entry-content">
                    <?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentytwelve' ) ); ?>
                    <?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'twentytwelve' ), 'after' => '</div>' ) ); ?>
                </div><!-- .entry-content -->
                <?php endif; ?>

                </div>

    </footer>
    </header><!-- .entry-header -->

2 个答案:

答案 0 :(得分:0)

老实说,你似乎已经知道如何做到这一点 - has_post_thumbnail()

例如:

if ( has_post_thumbnail() ) {
  echo '<div class="thumb-only">';
}

// some code

if ( has_post_thumbnail() ) {
  echo '</div>';
}

只要此代码位于Loop内,它就可以正常工作。

答案 1 :(得分:0)

if( wp_attachment_is_image( $post_id ) ){ ... }   

您也可以将其用于条件