Home > Software engineering >  i'm getting "syntax error, unexpected token "endif", expecting end of file"
i'm getting "syntax error, unexpected token "endif", expecting end of file"

Time:09-17

i'm building a theme but not knowing php is easy for me to get into syntax errors. I created this loop to display all posts from a custom post type named "disturbo". I want to display the title, the thumbnail and a custom field named "citazione" that has a tagline/quote.

I get the error

"syntax error, unexpected token "endif", expecting end of file"

what is wrong in my code?

Di cosa mi occupo
       <?php $args = array(
            'post_type' => 'disturbo',
            'category_name' => 'disturbo' ,
            'orderby' => 'title' ,
            'order' => 'DESC',
            'posts_per_page' => -1 );
             ?>
       <?php $disturbohome = new WP_Query($args); ?>

       <?php if ( $disturbohome->have_posts() ) :  
        while ( $disturbohome->have_posts() ) : 
            $disturbohome->the_post();      ?>
         
        <li>
            <a class="flex" href="<?php get_permalink(); ?>">
                <?php the_post_thumbnail( 'full' , ['class' => 'img-terapia'] ); ?>
            
                <div>
                    <?php the_title( '<h4 class="bluetext">', '</h4>' ); ?>
                <p class="tagline bluetext"><?php if ( get_post_meta( get_the_ID(), 'citazione', true ) ) : ?>
                        <p class="tagline bluetext"><?php echo ( get_post_meta( get_the_ID(), 'citazione', true ) ); ?>
                        <?php endif; ?>
                        </p>
                        <a href="<?php get_permalink(); ?>" class="more">learn more</a>
            </a>
        </li>
        <?php  endif;?>
        <?php endwhile; // end of the loop. ?>

                 
            
        <?php wp_reset_postdata(); ?>

        </ul>
    </nav>
</div><!--- FINE DIV CENTER -->   

CodePudding user response:

You have incorrectly nested the while loop. The beginning and end of the while loop needs to be 'nested' inside of the if condition.

       <?php $args = array(
            'post_type' => 'disturbo',
            'category_name' => 'disturbo' ,
            'orderby' => 'title' ,
            'order' => 'DESC',
            'posts_per_page' => -1 );
             ?>
       <?php $disturbohome = new WP_Query($args); ?>

       <?php if ( $disturbohome->have_posts() ) :
        while ( $disturbohome->have_posts() ) :
            $disturbohome->the_post();      ?>

        <li>
            <a class="flex" href="<?php get_permalink(); ?>">
                <?php the_post_thumbnail( 'full' , ['class' => 'img-terapia'] ); ?>

                <div>
                    <?php the_title( '<h4 class="bluetext">', '</h4>' ); ?>
                <p class="tagline bluetext"><?php if ( get_post_meta( get_the_ID(), 'citazione', true ) ) : ?>
                        <p class="tagline bluetext"><?php echo ( get_post_meta( get_the_ID(), 'citazione', true ) ); ?>
                        <?php endif; ?>
                        </p>
                        <a href="<?php get_permalink(); ?>" class="more">learn more</a>
            </a>
        </li>
        <?php endwhile; // end of the loop. ?>
        <?php  endif;?>



        <?php wp_reset_postdata(); ?>

        </ul>
    </nav>
</div><!--- FINE DIV CENTER -->
  • Related