• Hello, my posts on homepage are not being displayed, it only displays one post, I am so confused, I have more than one post on my website, I can’t figure out the reason why it displays only one post.

    <div class="container"> <!-- container start -->
          <div class="row overflow-hidden"> <!-- row start -->
           <?php
             if ( have_posts() ) :
                while ( have_posts() ) : the_post();
                  // display post content
                    endwhile;
                      else :
                        // no posts found
                          endif;
    		                    ?>
                  <div class="col-xs-6 col-sm-6 col-md-6 col-lg-4 col-xl-3"> <!-- columns start -->
                    <a href="<?php the_permalink(); ?>">
                      <div class="title-box mx-auto mb-0">
                        <h1 class="title-color">
                            <?php
                          $title = get_the_title();
                          if ($title):
                            echo the_title();
                          else:
                            echo "No Title";
                          endif;
                          ?>
                        </h1>
                        <?php
                            if( has_post_thumbnail() ):
                                the_post_thumbnail('full', [ 'title' => 'Feature image', 'class' => 'img-fluid']);
                            else:
                        ?>        
                            <img class="img-fluid" src="<?php echo get_template_directory_uri() . "/images/bg.jpg"; ?>">
                        <?php endif; ?>    
                      </div>
                    </a>    
                  </div> 
        
          </div>
        </div>

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • Anonymous User 14808221

    (@anonymized-14808221)

    … your code clearly jumps the loop and outputs just one post.

    See if ( have_posts() ) : (starts the loop), then immediately after without further ado endwhile;. So it is already done, the loop is closed, but no content was shown.

    Your single post you see is the one that you add after all that, in the div class col-xs-6
    But as it is outside that loop, it won’t repeat for each post found.

    You should do something like this:

    
    if ( have_posts() ) {
    	while ( have_posts() ) {
    		the_post(); 
    		// Here you put your stuff you want to show for EACH post, such as title, content, and HTML to wrap those contents.
    	} // end while
    } // end if
    
    Thread Starter ferdalkrmn

    (@ferdalkrmn)

    @bedas I tried but then I got problems to close my div tags

    Anonymous User 14808221

    (@anonymized-14808221)

    I do not fully understand what you mean.

    Can you elaborate? Maybe with a sample of code you use, which does not allow you to close div tags?

    Thread Starter ferdalkrmn

    (@ferdalkrmn)

    @bedas this is the code that I am trying to save, but WordPress admin panel does not allow me to do so. I don’t know where I am making the mistake.

     <div class="container"> <!-- container start -->
          <div class="row overflow-hidden"> <!-- row start -->
         <?php if ( have_posts() ) : while( have_posts()  ) : the_post(); ?>
                  <div class="col-xs-6 col-sm-6 col-md-6 col-lg-4 col-xl-3"> <!-- columns start -->
                    <a href="<?php the_permalink(); ?>">
                      <div class="title-box mx-auto mb-0">
                        <h1 class="title-color">
                            <?php
                          $title = get_the_title();
                          if ($title):
                            echo the_title();
                          else:
                            echo "No Title";
                          endif;
                          ?>
                        </h1>
                        <?php
                            if( has_post_thumbnail() ):
                                the_post_thumbnail('full', [ 'title' => 'Feature image', 'class' => 'img-fluid']);
                            else:
                        ?>        
                            <img class="img-fluid" src="<?php echo get_template_directory_uri() . "/images/bg.jpg"; ?>">
                        <?php endwhile; endif; ?>
                      </div>
                    </a>    
                  </div> 
          </div>
        </div> 
    • This reply was modified 1 year, 10 months ago by ferdalkrmn.
    Anonymous User 14808221

    (@anonymized-14808221)

    You cannot save PHP in the WordPress post editor, if that is what you mean.

    If anything, you would be adding that to the Theme PHP files.

    In the Theme editor, when you save PHP, and it fails, usually it tells you why.

    As far I saw your code there where at least some bad closed Divs (outside the actual loop instead of inside).
    Not sure if that would throw an error, as I usually edit theme files in an IDE and not in the backend, and you should get familiar with this too, since editing files in the backend, without access to the server, or/and debug turned on, can result in fatal errors that then disallow you complete access to the backend.

    So what I would suggest doing is:
    – get familiar with an IDE
    – make sure you have FTP (better SFTP) access to the server
    – create a child theme and edit the particular file where you want to add this code
    – save the file and check, while having debug turned on

    I edited your code a bit, adding some further security checks, some formatting, and corrected the badly closed DIVs, so now this should work, but please understand that I did not test this code on a site.

    
    // here comes anything before your loop such as get_header()
    ?>
    <div class="container"> <!-- container start -->
    	<div class="row overflow-hidden"> <!-- row start -->
    		<?php
    		/* Start the Loop */
    		while ( have_posts() ) {
    			the_post();
    			?>
    			<div class="col-xs-6 col-sm-6 col-md-6 col-lg-4 col-xl-3"> <!-- columns start -->
    				<a href="<?php the_permalink(); ?>">
    					<div class="title-box mx-auto mb-0">
    						<h1 class="title-color">
    							<?php
    								$the_title = get_the_title();
    							if ( $the_title ) {
    								echo esc_html( the_title() );
    							} else {
    								echo 'No Title';
    							}
    							?>
    						</h1>
    						<?php
    						if ( has_post_thumbnail() ) {
    							the_post_thumbnail(
    								'full',
    								array(
    									'title' => 'Feature image',
    									'class' => 'img-fluid',
    								)
    							);
    						} else {
    							?>
    								<img class="img-fluid" src="<?php echo esc_url_raw( get_template_directory_uri() . '/images/bg.jpg' ); ?>">
    								<?php
    						}
    						?>
    					</div>
    				</a>
    			</div>
    			<?php
    		} // End of the loop.
    		?>
    	</div>
    </div>
    <?php
    /// Here goes the rest of the template such as get_footer(), etc
    

    I also (as you can see in the code) would suggest using {} syntax and not if; endif; because it is much easier to debug, and IDEs can deal with this better than the older syntax.
    Hope this helps.

    Thread Starter ferdalkrmn

    (@ferdalkrmn)

    @bedas the code is just working perfectly, Thank you. I have one last question
    would it matter much if we don’t put if ‘( have_posts()’

    because in the code that you sent it starts from ‘while( have_posts()’ So I am just wondering the difference, I am a beginner at WordPress but trying to improve my knowledge ??

    Anonymous User 14808221

    (@anonymized-14808221)

    The ?if“ would basically just avoid the ?while“, if there are no posts
    It’s probably best practice to use it, as in ?exit early if there’s nothing to do“ to save some resources

    I just had adapted theme 2023 code for the example, and that doesn’t use the if.

    The while won’t break if you don’t use an if, since if there’s nothing to loop, it won’t loop.
    But a tiny fraction of a millisecond in processing power is used so the if would avoid that.

    • This reply was modified 1 year, 10 months ago by Anonymous User 14808221.
    Anonymous User 14808221

    (@anonymized-14808221)

    Meh, forget that.

    The have_posts() obviously you want to use for the “if have no posts” case (such as “nothing found”)
    Not sure why my local copy of the theme doesn’t include it, its either an error in the theme or I have perhaps messed with my copy earlier and forgot it.

    In any case, the “if have_posts()” would go through the “while have posts” loop, and the other case (else {}) would output something like “echo “no posts found”

    Hope that is clear, and sorry for confusing you!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘posts are not displayed’ is closed to new replies.