0

I've a problem with the loop in wordpress, my website is a feed aggregator and basically I want my website to display an image if the feed contains an image or a specified image if it doesn't. here's the code I actually use:

    <?php $enclosure =  get_post_meta($post->ID , 'enclosure', $single = true); ?>
    <?php $image=explode(chr(10),$enclosure); ?>
    <?php if(!is_null($image)) : ?>
    <div class="left">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Link all'articolo: <?php     the_title_attribute(); ?>"><img src="<?php echo $image[0]; ?>" alt="<?php the_title_attribute(); ?>" style="width:150px; text-align:center;"/></a>
    </div>
    <?php endif; ?>

It works very well but now I want to show a "default" image if the feed doesn't provide one, I tried in this way:

    <?php $enclosure =  get_post_meta($post->ID , 'enclosure', $single = true); ?>
    <?php $image=explode(chr(10),$enclosure); ?>
    <?php if(!is_null($image)) : ?>
    <div class="left">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Link all'articolo: <?php     the_title_attribute(); ?>"><img src="<?php echo $image[0]; ?>" alt="<?php the_title_attribute(); ?>" style="width:150px; text-align:center;"/></a>
    </div>
    <?php else : ?>
    <div class="left">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Link all'articolo: <?php     the_title_attribute(); ?>"><img src="PATH TO THE DEFAULT IMAGE" alt="<?php the_title_attribute(); ?>" style="width:150px; text-align:center;"/></a>
    </div>
    <?php endif; ?>

But it doesn't work... Where am I wrong? Sorry for my english!

Thanks!

6
  • use empty function instead of is_null, Commented Jul 26, 2013 at 7:40
  • Just FYI, you don't need to open and close php tags on every line... Commented Jul 26, 2013 at 7:40
  • as @SumitGupta suggest, use !empty($image) instead of !is_null($image) Commented Jul 26, 2013 at 7:40
  • Hi. If a question was answered, you should accept the answer. Commented Jul 26, 2013 at 7:41
  • You can put more than one statement inside a php block, do not open and close php blocks for each statement. Commented Jul 26, 2013 at 7:41

2 Answers 2

1

try

if(file_exists("file path")){

}

instead of

`if(!is_null($image)) :`
Sign up to request clarification or add additional context in comments.

Comments

0

try to use

if($image=="" && $image=="null")
{

}

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.