0

I have a PHP if else statement however it does not seem to work and I'm not sure why.

Here is my code

<?php 
    if (has_post_thumbnail()) {
?> 
        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php 
        the_post_thumbnail('full', array('class' => 'img-responsive alignleft')); 
?>
</a>
<?php 
    } else () {
?>
        <img src="<?php echo get_template_directory_uri(); ?>/images/thumb.png" />';
<?php 
    }
?> 

And here is the error that I get syntax error, unexpected ')'

I'm not sure where the unexpected ) is coming from.

I am basing my PHP on this structure, however editing it as I would like to be able to put mine into HTML without using echo

<?php if ( '' != get_the_post_thumbnail() ) {
// some code
} 
else {
// some code
}
?>
1
  • 3
    I'm pretty sure an empty elseif isn't allowed in PHP. Neither else () is. Commented Apr 22, 2014 at 15:26

4 Answers 4

3

Let's try with this:

<?php if (has_post_thumbnail()): ?> 
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
        <?php the_post_thumbnail('full', array('class' => 'img-responsive alignleft')); ?>
    </a>
<?php else: ?> 
    <img src="<?php echo get_template_directory_uri(); ?>/images/thumb.png" />';
<?php endif; ?>
Sign up to request clarification or add additional context in comments.

Comments

2

I'm not sure why you wrote your code this way instead of concatenating it, but your if else statement has two parenthesis after else "()". You might want to get rid of these to fix your issue.

Comments

0

In PHP else is not a function, so you shouldn't call it like you did.

Comments

0

PHP's elseif () needs conditions,
try else instead:

<?php 
    if ( has_post_thumbnail() ) { 
?>
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail( 'full', array( 'class' => 'img-responsive alignleft' ) ); ?></a>
<?php 
    } else { //instead of elseif () here, use else
?> 
    <img src="<?php echo get_template_directory_uri(); ?>/images/thumb.png" />
<?php 
    }
?>

Please also refer to the WordPress Coding Standards to make your code easier readable and maintainable. And try not to edit the original code in your question without marking the edits clearly, otherwise SO users won't be able to understand your problem/help you further.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.