0

I'm trying to add a conditional that will display a default image if the "image" field isn't populated... here's what I'm trying to work with:

$args = array(
   'posts_per_page' => 3,
   'post_type' => 'post',
   'meta_query' => array( 
      array(
         'key' => 'is_featured', 
         'value' => '1',
         'compare' => '!=', // NOT Featured
      )
   )
);

$second_query = new WP_Query( $args );
if ( $second_query->have_posts() ):
   while( $second_query->have_posts() ) : $second_query->the_post(); 

   $attachment_id = get_field('image');
   $size = "customfeatins"; // (thumbnail, medium, large, full or custom size)
   $image = wp_get_attachment_image_src( $attachment_id, $size );

        echo '<article>

            <img src="' . $image[0] . '" alt="' . get_the_title() .'" width="136" height="90" />
            <h3>' . get_the_title() .'</h3>
            <p class="date">' . get_the_date() .'</p>

        </article>
        ';

   endwhile;
endif;
wp_reset_postdata();

I've tried this but it breaks the page:

$args = array(
   'posts_per_page' => 3,
   'post_type' => 'post',
   'meta_query' => array( 
      array(
         'key' => 'is_featured', 
         'value' => '1',
         'compare' => '!=', // NOT Featured
      )
   )
);

$second_query = new WP_Query( $args );
if ( $second_query->have_posts() ):
   while( $second_query->have_posts() ) : $second_query->the_post(); 

  if( get_field('image') ){

    $attachment_id = get_field('image');
    $size = "customfeatins"; // (thumbnail, medium, large, full or custom size)
    $image = wp_get_attachment_image_src( $attachment_id, $size );

    } else {

    $image = wp_get_attachment_image_src( 'http://placehold.it/136x90' );

    }       

    endif;

        echo '<article>

            <img src="' . $image[0] . '" alt="' . get_the_title() .'" width="136" height="90" />
            <h3>' . get_the_title() .'</h3>
            <p class="date">' . get_the_date() .'</p>

        </article>
        ';

   endwhile;
endif;
wp_reset_postdata();

3 Answers 3

0

try this

$second_query = new WP_Query( $args );
if ( $second_query->have_posts() ):
   while( $second_query->have_posts() ) : $second_query->the_post(); 

    $attachment_id = get_field('image');
    $size = "customfeatins"; // (thumbnail, medium, large, full or custom size)
    $my_image = wp_get_attachment_image_src( $attachment_id, $size );


    if (!my_image !== ''){ 

      $image = $my_image[0];

    } else {

      $image = 'http://domain/image_source/your_image.jpg';

    }       

    endif;

        echo '<article>

            <img src="' . $image . '" alt="' . get_the_title() .'" width="136" height="90" />
            <h3>' . get_the_title() .'</h3>
            <p class="date">' . get_the_date() .'</p>

        </article>
        ';

   endwhile;
endif;
1
  • Thanks for helping, this didn't work though, not sure why but it broke the page... Nov 30, 2012 at 18:40
0

I'm pretty sure this won't work:

$image = wp_get_attachment_image_src( 'http://placehold.it/136x90' );

As wp_get_attachment_image_src( $id ) requires an attachment ID, as an integer, to be passed as $id.

If you have a fallback default image in the Theme, you can call it like so:

$image = get_template_directory_uri() . '/images/fallback.png';

Or, if you want the user to select an image from the media library to use, you'll need to query that image by $id. How you do that will depend on how you store the user's selection.

Edit

the fall back is almost working, however it's only outputting the first character in the source, e.g.: src="/"

That's because in one case, you're putting an array inside of $image, and in the fallback, you're putting a string inside of $image. But in your output, you reference $image[0], which implies that $image is an array.

I would recommend passing $image[0] into a separate variable, perhaps $image_src. Then, you can reference $image_src in the fallback case, and then also output $image_src.

For example:

if( get_field('image') ){
    // stuff
    $image = wp_get_attachment_image_src( $attachment_id, $size );
    // ADD THIS
    $image_src = $image[0];
} else {
    // USE $image_src HERE AS WELL
    $image_src = get_template_directory_uri() . '/images/fallback.png';
}
// more stuff
// ...skipping ahead
// ...

// USE $image_src HERE AS WELL
<img src="' . $image_src . '" alt="' . get_the_title() .'" width="136" height="90" />
2
  • Chip - the fall back is almost working, however it's only outputting the first character in the source, e.g.: src="/" Nov 30, 2012 at 18:35
  • See updated answer, with explanation and example code Nov 30, 2012 at 18:45
0

I finally got this working - thanks everyone for the feedback, with that I was able to rethink things a little and figure out a solution that does just what I wanted it to :)

Here it is (also filtering through tags to get related posts):

<?php
                    $tags = wp_get_post_tags($post->ID);
                        $tag_ids = array();
                        foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;

                        $args = array(
                            'tag__in' => $tag_ids,
                            'post_type' => 'post',
                            'post__not_in' => array($post->ID),
                            'posts_per_page' => 3, // Number of related posts that will be shown.
                            'caller_get_posts'=> 1
                    );  

                    $second_query = new WP_Query( $args );
                    if ( $second_query->have_posts() ):
                        echo '<div class="title fs-60 salmon">Related Posts</div>';
                        while( $second_query->have_posts() ) : $second_query->the_post();

                        $titlechars = 45; // Character Limit
                        $posttitle = get_the_title();
                        $modtitle = substr($posttitle, 0, $titlechars);

                        $contentchars = 120; // Character Limit
                        $postcontent = get_the_excerpt();
                        $modcontent = substr($postcontent, 0, $contentchars);

                    echo '<article>';

                ?>

                <?php

                    if( get_field('image') ):

                        $attachment_id = get_field('image');
                        $size = 'customfeatins'; // (thumbnail, medium, large, full or custom size)
                        $image = wp_get_attachment_image_src( $attachment_id, $size );

                    echo '<img src="' . $image[0] . '" alt="' . get_the_title() .'" width="136" height="90" />';

                ?>

                <?php else : ?>

                    <img src="http://placehold.it/136x90" alt="image" />

                <?php endif; ?>

                <?php

                    echo '
                        <h3 class="purple">' . $modtitle .'</h3>
                        <p class="date">' . get_the_date() .'</p>
                        <p>' . $modcontent . '&hellip; <a href="' . get_permalink() . '">More &rsaquo;</a></p>

                        </article>';

                ?>
                <?php 
                    endwhile;
                    endif;
                    wp_reset_postdata();

?>  

This uses the placeholder image (set as placehold.it for now) if the post doesn't have an image in the custom image field.

Your Answer

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

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