Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
92 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 Reply

0 votes
by (71.8m points)

You need to do this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...