Question

Is there a way to tell the wordpress content page to show a div around content if there is a featured image and remove there div if there is not? I code Html and css but php is a blur to me. have mercy :) I currently have it to place a dummy thumbnail in place of a featured image if it does not exist. see below. thanks!!

<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 -->
Was it helpful?

Solution

You seem to already know how to do this, honestly-- has_post_thumbnail().

For example:

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

// some code

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

So long as this code is inside a Loop, it should work.

OTHER TIPS

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

you can also use this for condition

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top