Home > Net >  loop problem in category.php page in wordpress when using "while (have_posts()) : the_post();&q
loop problem in category.php page in wordpress when using "while (have_posts()) : the_post();&q

Time:02-21

I want to loop <div > using while (have_posts()) : the_post();> Problem is this loop create another <div > inside the first <div >and I lost to create 2 column section. Below is my code

Getting this output:

<div >
    <div >
        Post 1 details
    </div>
    <div >
        Post 2 details
    </div>
</div>

I want to get this output:

<div >
    Post 1 details
</div>
<div >
    Post 2 details
</div>



<section >
        <div >
            <div >
                
                <?php if (have_posts()) : ?>
                <?php while (have_posts()) : the_post(); ?>
                <div >
                    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                    <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
                    <div >
                        <?php the_content(); ?>

                        
                    </div>

                    <?php endwhile; 

                    else: ?>
                    <p>Sorry, no posts matched your criteria.</p>


                    <?php endif; ?>
                </div>  
            </div>
        </div>
    </section>

CodePudding user response:

You close the <div > outside of the loop section. Your fixed code is:

<section >
    <div >
        <div >
            <?php if (have_posts()) : ?>
                <?php while (have_posts()) :
                    the_post(); ?>
                    <div >
                        <h2><a href="<?php the_permalink() ?>" rel="bookmark"
                               title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                        <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
                        <div >
                            <?php the_content(); ?>


                        </div>
                    </div>
                <?php endwhile;

            else: ?>
                <p>Sorry, no posts matched your criteria.</p>
            <?php endif; ?>
        </div>
    </div>
</section>
  • Related