• Hello,

    Before I throw my computer out of the window could somebody help me with the following code that I wrote (the if and else statement was provided by another kind user here). For some reason when I try to output HTML I am confronted with a parsing error. This happens inside the if and else statements:

    <?php
    
    get_header('topbar'); ?>
    	
    <div class="blog_container">
    		<h2 class="blog-title">
    		Latest News
    		</h2>
    	<p>
    		News information and much more.
    	</p>
    </div>
    
    <?php
    
    $posts_query = new WP_Query( $query_args );
    
    if ( $posts_query->have_posts()) { 
    
      $first_post = true;
      while ( $posts_query->have_posts() ) { 
        $posts_query->the_post();
        
        if ( $first_post ) {
         <h2>This is where my latest post will appear</h2>
        } else {
          <h2>This is where all subsequent posts should appear</h2>
        }
    
        $first_post = false;
      } 
    
    }
    
    wp_reset_postdata();
    ?>
    		
    <?php 
    ?>
    <?php get_footer();
    
    ?>
Viewing 11 replies - 1 through 11 (of 11 total)
  • You need to echo this out using php, or close your php tags:
    <h2>This is where my latest post will appear</h2>

    Like this:

    if ( $first_post ) {
         echo '<h2>This is where my latest post will appear</h2>';
        } else {
          echo '<h2>This is where all subsequent posts should appear</h2>';
    }
    Thread Starter digstertron

    (@digstertron)

    Tried both options the problem persists.

    Thread Starter digstertron

    (@digstertron)

    I realise it’s something I’ve done but this is madenning, is this code correct?

    <?php
    
    get_header('topbar'); ?>
    	
    <div class="blog_container">
    		<h2 class="blog-title">
    		Latest News
    		</h2>
    	<p>
    		News information and much more.
    	</p>
    </div>
    
    <?php 
    $posts_query = new WP_Query( $query_args );
    
    if ( $posts_query->have_posts()) { 
    
      $first_post = true;
      while ( $posts_query->have_posts() ) { 
        $posts_query->the_post();
        
        if ( $first_post ) {
         echo '<h2>This is where my latest post will appear</h2>';
        } else {
          echo '<h2>This is where all subsequent posts should appear</h2>';
    }
    
        $first_post = false;
      } 
    
    }
    
    wp_reset_postdata();
    
    ?>
    	
    
    <?php get_footer();
    
    ?>

    Then you’re doing something else wrong.

    The syntax is fine after you add the echo in your print statements:
    https://postimg.cc/xq14YsZ7
    https://phpcodechecker.com

    I also tested it on my own development server.

    Yes, what you’ve posted back is correct.

    Copy and paste the error.

    Thread Starter digstertron

    (@digstertron)

    There is no error now only the echoed out content does not appear at all. All I see is the header and footer.

    Well then one of your conditions is not being met:
    if ( $posts_query->have_posts()) {
    or
    while ( $posts_query->have_posts() ) {
    if the post_query is empty:
    $posts_query = new WP_Query( $query_args );

    Thread Starter digstertron

    (@digstertron)

    Here is what I see:

    Screen grab

    Just check the values right away coming from the post_query :
    $posts_query = new WP_Query( $query_args );

    Maybe you’re not inside the loop.
    Just run this:
    https://developer.www.ads-software.com/reference/functions/query_posts/#usage

    Thread Starter digstertron

    (@digstertron)

    The queries were both met, I added relevant HTML to both the if and else statement.

    I’ll come back to it tomorrow. Completely mind numbingly irritating, all I want to do is display the very latest post at the top of the blog page with the remaining posts in a row below. I’ve found writing HTML and CSS quite easy but this PHP is another level of nonsense ??

    Thanks ever so much for your help though.

    • This reply was modified 3 years, 2 months ago by digstertron.
    Dion

    (@diondesigns)

    Here’s your main PHP code rewritten to display debugging info. First, note the comment on the first line…if $query_args isn’t defined, the line will generate a notice in PHP7 and a warning in PHP8. If the “no posts” line is displayed, then the query failed. If “this is post number” lines are displayed with a post ID of zero, then something is wrong in the query.

    <?php 
    $posts_query = new WP_Query($query_args); // where is $query_args defined?
    $post_count = 0;
    
    if ($posts_query->have_posts()) { 
    	while ($posts_query->have_posts()) { 
    		$post_count++;
    		$posts_query->the_post();
    		$post_id = (int) get_the_ID();
    		echo "<h2>This is post number $post_count with ID $post_id</h2>";
    	}
    } 
    
    if (!$post_count) {
    	echo '<h2>There were no posts!</h2>';
    }
    
    wp_reset_postdata();
    get_footer();
Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Parsing errors & if statements not working.’ is closed to new replies.