Whilst looking through the codex for the get_posts
function, I noticed that in the majority of the examples they do not show an if statement after call to get_posts
Example:
<?php
$args = array( 'posts_per_page' => 10, 'order'=> 'ASC', 'orderby' => 'title' );
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
setup_postdata( $post ); ?>
<div>
<?php the_date(); ?>
<br />
<?php the_title(); ?>
<?php the_excerpt(); ?>
</div>
<?php
endforeach;
wp_reset_postdata();
?>
And:
<?php
$args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' =>'any', 'post_parent' => $post->ID );
$attachments = get_posts( $args );
**if ( $attachments ) {**
foreach ( $attachments as $attachment ) {
echo apply_filters( 'the_title' , $attachment->post_title );
the_attachment_link( $attachment->ID , false );
}
}
?>
I'm not sure if the codex is assuming that you will always have posts so no need to check?
I personally always use it, but wonder if someone could enlighten me on if its necessary. When is a right time to use the if statement?