1

Can someone help me with a PHP line in Wordpress.

I just add the PHP else condition after the if in case that the site owner did not post a feature image. Please don't correct it but point me a hint so I can find the syntax error myself.

<a class="main_img" href="<?php the_permalink();?>">
<?php $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );?>
<?php if($feat_image){?>
<img src="<?php echo $feat_image; ?>" alt="">
<?php } ?> 

<?php else() {?>
<img src="http://website.com/if_no_feat_image.jpg" alt="">
<?php } ?>

</a>
3
  • An else statement does have a requirement, it just does whatever the if statement doesn't do. You need to remove the () after the else statement in order for it to work. If you however want the else statement to have a condition change it to elseif then you can have the () to put the condition in. Commented Apr 12, 2016 at 21:28
  • OP: Thank you for asking for a hint so you could find the error itself, instead of asking for the code outright. That is are rare and welcome attitude from newbies indeed! Just for that, let me give you a bit of advice from a pro: If you've written any amount of working, practical code, you're a "real programmer". Inexperienced as of yet, but real. Many old coding vets even ask themselves if they're "real" - it isn't worth the emotional exhaustion. (I also bring that up b/c I edited it out of the question for brevity.) Commented Apr 13, 2016 at 0:05
  • On another note, in the future, please include the full text of the error or a proper description of the problem. It's vital, especially as code gets more complex. See How to Ask for more info. Commented Apr 13, 2016 at 0:07

2 Answers 2

1

Your syntax is wrong for the else part. You don't need the parenthesis.

Change <?php else() {?> to this: <?php else {?>

Sign up to request clarification or add additional context in comments.

Comments

0

The correct form:

<a class="main_img" href="<?php the_permalink();?>">
    <?php $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );?>
    <?php if ($feat_image) { ?>
        <img src="<?php echo $feat_image; ?>" alt="">
    <?php } else { ?>
        <img src="http://website.com/if_no_feat_image.jpg" alt="">
    <?php } ?>
</a>

and a more beautiful one:

<a class="main_img" href="<?php the_permalink();?>">
    <?php $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );?>
    <?php if ($feat_image): ?>
        <img src="<?php echo $feat_image; ?>" alt="">
    <?php else: ?>
        <img src="http://website.com/if_no_feat_image.jpg" alt="">
    <?php endif; ?>
</a>

3 Comments

So I add to change this : <?php else() {?> to : <?php } else { ?>
Still do not know why some people use { } in php code and some dont. Here is the best example with your correct form and more beautiful one.
Usually syntax with {} is for back-end. And syntax with : is for front-end (view (inside html)).

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.