I have my first half of the code which is supposed to be post with thumbnail in classes. One is class item a and other is class item b. But after “else” the code takes the same class item a even if I haven’t put it around the code. Why does this happen?
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <div class="item a"> <?php if( has_post_thumbnail() ): ?> <a href="<?php the_permalink()?>"> <div class="thumbnail"><?php the_post_thumbnail('medium'); ?></div> </a> </div> <div class="item b"> <?php the_title( sprintf('<h3 class="entry-title"><a href="%s">', esc_url( get_permalink() ) ),'</a></h3>' ); ?> <?php $content = get_the_content(); $trimmed_content = wp_trim_words( $content, 33, NULL ); echo $trimmed_content; ?> </div> <?php else : ?> <?php the_title( sprintf('<h3 class="entry-title"><a href="%s">', esc_url( get_permalink() ) ),'</a></h3>' ); ?> <?php $content = get_the_content(); $trimmed_content = wp_trim_words( $content, 33, NULL ); echo $trimmed_content; ?> <?php endif; ?> </article>
The first article is with thumbnail and other is without thumbnail on the image.
Advertisement
Answer
One of your div
opening tag is outside your if
statement, and the closing tag is inside.
<div class="item a"> <?php if( has_post_thumbnail() ): ?> </div> <?php else: ?>
In the following snippet, I’ve corrected your error in a minimal version, just put your content back inside.
<article> <?php if( has_post_thumbnail() ): echo '<div class="item a"> <!-- content, item a.. --> </div> <div class="item b"> <!-- content, item b.. --> </div>'; else: // else, fallback... endif; ?> </article>