• Resolved commanderk84

    (@commanderk84)


    Hi,

    I just created my first WordPress theme and it works fine on my Wamp-server.
    But after uploading it to my site, I get an endif syntax error which is not occurring on my localhost.

    The code for displaying a certain category posts:

    <?php if ( has_post_thumbnail() ) {
    	
    	// Get the post thumbnail URL
    	$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
    } else {
    	
    	// Get the default featured image in theme options
    	$feat_image = get_field('default_featured_image', 'option');
    } ?>
    
    <div class="col-xl-3 col-lg-3 col-md-6 col-sm-6 col-xs-12 square" style="background-image: url(<?php echo $feat_image; ?>);" onClick="location.href=' <?php the_permalink(); ?> ';" id="linkeddiv">
        <div class="marginsquareproject">
            <div class="show"><img src="http://localhost/wordpress/wp-content/themes/abcd/images/show.png" class="img-fluid showicon" alt="Placeholder image"></div> 
        </div>
    </div>
    
    <? endif; ?>

    Error:
    —–
    Jouw PHP-wijzigingen zijn teruggedraaid vanwege een fout op regel 21 in het bestand wp-content/themes/abcd/content-project-front.php. Repareer en probeer opnieuw.

    syntax error, unexpected ‘endif’ (T_ENDIF), expecting end of file
    —–

    But I need the endif… what goes wrong here?

    • This topic was modified 1 month, 2 weeks ago by commanderk84.
Viewing 10 replies - 1 through 10 (of 10 total)
  • In the provided code I’m not seeing _why_ you need the endif;. PHP conditionals go one of two ways:

    if( conditional ) {
    
        // Code
    	
    } else {	// Optional else 
    
    	// Code
    	
    } // END conditional block

    OR

    <?php if( conditional ) : ?>
    
    	<!-- Non PHP code -->
    	
    <?php else : ?>		<!-- Optional else block -->
    	
    	<!-- Else code block -->
    	
    <?php endif; ?> <!-- END conditional block -->

    Every IF needs an ending conditional block, whether it’s simply a closing bracket } or a <?php endif; ?>. The problem is in the provided code I do not see an opening <?php if( conditional ) : ?>

    That is what I do right?

    I open a if condition with ‘if’ and at the end I do and endif.
    Tried it some other ways, but it keeps creating errors.

    Could you copy the code like it should be?

    The site works fine now as long as I do not create posts in this category at the moment.

    You should just be able to remove the <?php endif; ?> entirely. You do not have

    <?php if( conditional ) : ?>

    You have

    if( conditional ) {
    
    }

    The two are different conditional types.

    That works now…
    But then I get critical errors on another page where I make the initial query.

    Can you post the error and the code of the initial query? That may be a different error entirely, maybe the conditional was terminated at some point earlier, or maybe it’s how the theme is including files.

    I just get ‘A critital error has occured on your website’ on the projectpage.
    On this page I query posts for the project-template, whit the templatecode I posted before.

    The code for this page:

    <?php
    
    /*
    
        Template Name: Projectenpagina
        
    */
    
    ?>
    
    <?php get_header(); ?>
    
    <div class="container-fluid fullwidth">
    	  
      <div class="content">
    
      <div class="row first">
        <div class="col-xl-12 contentarea">
    		<div class="row menu">
    			
    			<div class="bvvlogo" onClick="location.href='http://www.123.nl/v2';" id="linkeddiv" alt="Terug naar beginpagina" title="Terug naar beginpagina"> <img src="http://www.123/v2/wp-content/themes/abcd/images/logo.png" class="img-fluid logo" alt="alt text"></div>
    			
    			<input type="checkbox" id="nav-toggle" class="nav-toggle">
    			<label for="nav-toggle" class="nav-toggle-label">
    				<div class="hamburger">
    				<div class="bar"></div>
    				<div class="bar"></div>
    				<div class="bar"></div>
    				</div>
    			</label>
    			
    			<nav>
    				<?php wp_nav_menu( array( 'theme_location' => 'header-menu' ) ); ?>
    			</nav>
    
    			
    		</div>
    		
    		
            
            
            <div class="row thecontent">
    
                    <?php 
    
                    $lastPortfolio = new WP_Query('type=post&cat=4&orderby=rand');
    
                    if ( $lastPortfolio->have_posts() ):
    
                        while( $lastPortfolio->have_posts() ): $lastPortfolio->the_post(); ?>
    
                            <?php get_template_part('content-project-projectpage', get_post_format()); ?>
    
                        <?php endwhile;
    
                    endif;
    
                    wp_reset_postdata();
              
                    wp_reset_query();
    
                    ?>
    
    	    </div>
    		
    
    	  </div>
      </div>
    
    	
    
    	
      </div>
    	  
    	  </div>
    
    <?php get_footer(); ?>

    Also tried to remove the endif and endwhile, but that didn’t work.

    Strange this all off this code work on Wamp-server, but not on internet

    • This reply was modified 1 month, 2 weeks ago by commanderk84.

    This mostly looks correct to me, nothing sticks out about a fatal error. Maybe enable debugging and debug logs to see exactly where the script breaks and get further details. Check out the docs on Debugging.

    That being said, when you’re doing a secondary query ( as you’re doing in the code above ) you only need wp_reset_postdata(); and you do not need wp_reset_query();. The latter function is only if you’re overwriting the global $wp_query variable which you are not doing by creating your own new WP_Query.

    The debugging helped a lot!
    Thank you for your help and quick response!

    Hi, did you find the problem through the debugging hints?

    If your question has been answered, we would love if you would mark this topic as resolved in the sidebar. This helps our volunteers find the topics that still need attention and more people will get helped, possibly like you did.

    The debuggings hint helped me. It was another endif on a other page.

Viewing 10 replies - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.