admin管理员组

文章数量:1323723

here is the code in single.php and I want to add the loop. anywhere I put endif, I am getting error where can I add it, please?

updated code:

and many thanks in advance.

<div class="row">
                        <div class="col-md-8">
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
                            <div class="blog-post">
                                <div class="single-post">
                                    <div class="post-thumb">"><img src="<?php the_post_thumbnail_url(); ?>" alt="blog thumb" /></div>
                                    <div class="blog-single-content">
                                        <div class="blog-list-content">
                                            <p class="post-meta">Posted By <a href="#"> <?php the_author(); ?> </a></p>
                                            <h3 class="blog-title"> <?php the_title(); ?> </h3>
                                                                    <?php the_content(); ?>
                                        </div> <!-- class="blog-list-content" -->                            
                                    </div> <!-- class="blog-single-content" -->
                                </div> <!-- class="single-post" -->
                            </div> <!-- class="blog-post" -->
                            <?php endif; ?>

                            <?php endwhile; ?>
                            <?php comments_template(); ?>

here is the code in single.php and I want to add the loop. anywhere I put endif, I am getting error where can I add it, please?

updated code:

and many thanks in advance.

<div class="row">
                        <div class="col-md-8">
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
                            <div class="blog-post">
                                <div class="single-post">
                                    <div class="post-thumb">"><img src="<?php the_post_thumbnail_url(); ?>" alt="blog thumb" /></div>
                                    <div class="blog-single-content">
                                        <div class="blog-list-content">
                                            <p class="post-meta">Posted By <a href="#"> <?php the_author(); ?> </a></p>
                                            <h3 class="blog-title"> <?php the_title(); ?> </h3>
                                                                    <?php the_content(); ?>
                                        </div> <!-- class="blog-list-content" -->                            
                                    </div> <!-- class="blog-single-content" -->
                                </div> <!-- class="single-post" -->
                            </div> <!-- class="blog-post" -->
                            <?php endif; ?>

                            <?php endwhile; ?>
                            <?php comments_template(); ?>
Share Improve this question edited Sep 2, 2020 at 18:12 social asked Sep 2, 2020 at 17:46 socialsocial 713 silver badges9 bronze badges 4
  • 1 From the looks of it, you're already in The Loop. Functions like the_*() are a good indication of this. Some themes skip The Loop on single posts since there's one post to display and it's already loaded. – Howdy_McGee Commented Sep 2, 2020 at 18:02
  • thanks for your comment. but in every theme I do not know where to put endif and endwhile. it always gets error. so I want to understand how to add it without getting error – social Commented Sep 2, 2020 at 18:05
  • 1 Maybe edit your question with an attempt and users can point out your error. Some good examples are the early Twenty X themes. For example, Twenty Seventeen single.php. In the above example you wouldn't need to put a loop if you're already in the loop. Also some helpful resources with lots of examples: The Codex & The Handbook – Howdy_McGee Commented Sep 2, 2020 at 18:09
  • @ Howdy_McGee thanks for your comment. what do you mean by I am already in the loop, please? and I updated the code with the loop – social Commented Sep 2, 2020 at 18:17
Add a comment  | 

1 Answer 1

Reset to default 0

It appears there is a syntax error with this line:

if ( have_posts() ) while ( have_posts() ) : the_post();

You are missing a colon after ( have_posts() ).

If it's not a theme related issue as mentioned in the comments if you change it to: "if ( have_posts() ) : while ( have_posts() ) : the_post();"

Also, the endwhile should come BEFORE the endif.

After making these changes it should work.

See Example Here:

<div class="row">
                        <div class="col-md-8">
<?php  if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                            <div class="blog-post">
                                <div class="single-post">
                                    <div class="post-thumb">"><img src="<?php the_post_thumbnail_url(); ?>" alt="blog thumb" /></div>
                                    <div class="blog-single-content">
                                        <div class="blog-list-content">
                                            <p class="post-meta">Posted By <a href="#"> <?php the_author(); ?> </a></p>
                                            <h3 class="blog-title"> <?php the_title(); ?> </h3>
                                                                    <?php the_content(); ?>
                                        </div> <!-- class="blog-list-content" -->                            
                                    </div> <!-- class="blog-single-content" -->
                                </div> <!-- class="single-post" -->
                            </div> <!-- class="blog-post" -->
                            <?php endwhile; ?>
                            <?php endif; ?>

                          
                            <?php comments_template(); ?>

I always check my PHP with a code checker here: https://phpcodechecker/. I have found it helps a lot when debugging my code. Hopefully, this works for you!

本文标签: how to loop through this in blog single