Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How can i retrieve a picture, let say ( No picture ) when i don't add a picture in my custom field

I use this code to retrive the field...

<img class="film" 
     src="/scripts/timthumb.php?src=<?php the_field('img_actor'); ?>&h=251&w=175&zc=1"
     alt="<?php the_title(); ?>"  
     title="<?php the_title(); ?>"/>

How to retrive no-picture from this code

<?php 
if(get_field('distributie') != ""){$counter=0;foreach(get_field('distributie') as $post_object): ?>
    <div class="act-box">
<a href="<?php echo get_permalink($post_object->ID); ?>" title="Actor <?php echo get_the_title($post_object->ID) ?>">
<img class="actor_img" src="/scripts/timthumb.php?src=<?php echo get_field( 'img_actor', $post_object->ID ); ?>&h=70&w=50&zc=1" alt="<?php echo get_the_title($post_object->ID) ?>" title="<?php echo get_the_title($post_object->ID) ?>" /></a>
        <span class="act-titlu">
<a href="<?php echo get_permalink($post_object->ID); ?>" title="Actor <?php echo get_the_title($post_object->ID) ?>"><?php echo get_the_title($post_object->ID) ?></a>
        </span>
     </div>
<?php $counter++;if($counter>=8) break; endforeach;}?>

If i change

<?php echo get_field( 'img_actor', $post_object->ID ); ?>

with

<?php echo urlencode($img); ?>

I get an error ($post_object->ID) where do i put it?

share|improve this question
    
the_field is not a WordPress function, but I'll assume you can check if it's empty or not using a conditional. –  Wyck Jan 2 '13 at 20:02
    
i know how to retrive a picture, the question si how to get a default picture, if no picture is added.. –  Lucica Jan 2 '13 at 20:07
    
And where does this default picture come from? –  Wyck Jan 2 '13 at 20:08
    
The picture is in my www.mysite.com/wp-content/uploads/default.jpg , i added trought FTP ... if there is no picture i want to automate get the default picture –  Lucica Jan 2 '13 at 20:10
add comment

2 Answers

up vote 0 down vote accepted

Test the value before you use it:

<?php
if ( ! $img = get_field('img_actor', get_the_ID() ) )
    $img = 'http://www.example.com/wp-content/uploads/default.jpg';
?>
<img class="film" 
     src="/scripts/timthumb.php?src=<?php echo urlencode($img); ?>&h=251&w=175&zc=1"
     alt="<?php the_title_attribute(); ?>"  
     title="<?php the_title_attribute(); ?>"/>

I recommend not to use the same text for title and alt, this is very annoying for screen reader users.

share|improve this answer
    
perfect it works...thanks toscho –  Lucica Jan 2 '13 at 20:34
    
Toscho, please look at the first post... –  Lucica Jan 2 '13 at 20:42
    
@LucicaCopil Use get_the_ID() to get the post ID. –  toscho Jan 2 '13 at 20:45
    
if i use that i get all the post owerite with the default picture... –  Lucica Jan 2 '13 at 20:51
    
$img = get_field('img_actor', $post_object->ID - solve the problem... thanks again –  Lucica Jan 2 '13 at 20:55
add comment

Here is what I use for image redundancy.

1.First it tries to get the featured image
2.Then the first image attached to the post
3.Then a default image.

 $attachments = get_children(
                             array(
                                   'numberposts' => 1,
                                   'order'=> 'ASC',
                                   'post_mime_type' => 'image',
                                   'post_parent' => get_the_ID(),
                                   'post_status' => null,
                                   'post_type' => 'attachment'
                                   ));

        //first check for featured image
        if ( has_post_thumbnail() ) {

            the_post_thumbnail('thumbnail');

        //no featured image get first attachment   
        } elseif ($attachments) { 

        $first_attachment = reset($attachments);
            echo wp_get_attachment_image($first_attachment->ID, 'thumbnail');

        } else {
            echo '<img src="../something/default.jpg"'; 
        }

ps. You can insert the crap timthumb into it if you want or a 4th check for your custom get_field('img_actor')

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.