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();