Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
93 views
in Technique[技术] by (71.8m points)

php - Add a div inside wordpress loop?

I am a relatively new developer, and just started with wordpress. I want for each post to be wrapped in a class of post. I keep getting this error no matter what i do. If i echo the div or add post_class.

<div class="posts">
        <?php  
            $args = array( 
                'post_type'      => 'post',
                        'posts_per_page' => 4 
            ); 
            
            // Variable to call WP_Query. 
            $the_query = new WP_Query( $args ); 
            if ( $the_query->have_posts() ) : 
                // Start the Loop 
                while ( $the_query->have_posts() ) : $the_query->the_post(); 
                echo '<div class='post'>'
                    the_post_thumbnail();
                    the_title('<h2>','</h2>'); 
                    the_excerpt();
                echo '</div>'
                endwhile; 
                
            else: 
            // If no posts match this query, output this text. 
                _e( 'Sorry, no posts matched your criteria.', 'textdomain' ); 
            endif; 
            
            wp_reset_postdata(); 
        ?>
    </div>

enter image description here

question from:https://stackoverflow.com/questions/65650219/add-a-div-inside-wordpress-loop

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to do this:

echo '<div class="post">';
...
echo '</div>';

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...