Reputation: 34652
I am using this code, the best I can write without actually ever seeing an example or one close enough for my brain to understand.
This works, but it could improve. The problem is that I have a custom multicheck and its options are 'post' and/or 'page'. So I am checking the array and creating a variable. There must be another way.
function theme_prefix_featured_image() {
$show = get_theme_mod( 'auto_add_featured_image', array( 'post', 'page' ) );
$size = get_theme_mod( 'featured_image_size' );
if ( ! has_post_thumbnail() || ! is_singular() || empty( $show ) || empty( $size ) ) return;
$post_types = $caption = '';
/// HERE IS THE AREA BEGIN
if ( in_array( 'post', $show ) ) :
$post_types = is_singular( array ( 'post' ) );
endif;
if ( in_array( 'page', $show ) ) :
$post_types = is_singular( array ( 'page' ) );
endif;
if ( in_array( 'post', $show ) && in_array( 'page', $show ) ) :
$post_types = is_singular( array ( 'post', 'page' ) );
endif;
/////// HERE IS THE AREA END
//Get Caption
$caption = get_post( get_post_thumbnail_id() )->post_excerpt;
if ( $post_types ): //// TO CREATE THE CONDITIONAL
if ( ! empty( $caption ) ) :
$caption = sprintf( '<figcaption>%s</figcaption>', $caption );
endif;
$image = genesis_get_image( array(
'format' => 'html',
'size' => $size,
'context' => '',
'attr' => '',
) );
printf( '<figure class="featured-image aligncenter">%s%s</figure>', $image, $caption );
endif;
}
Upvotes: 0
Views: 50
Reputation: 1447
The following should be sufficient:
$post_types = is_singular( array_intersect( array ( 'post', 'page' ), $show ) );
Upvotes: 1
Reputation: 21681
Not sure if this is what you are asking, but you can combine the if statements, like this:
if ( in_array( 'post', $show ) ){
if ( in_array( 'page', $show ) ){
//in both post and page
$post_types = is_singular( array ( 'post', 'page' ) );
}else{
//in only post
$post_types = is_singular( array ( 'post' ) );
}
}else if ( in_array( 'page', $show ) ){
//in only page
$post_types = is_singular( array ( 'page' ) );
}else{
//undefined.
}
This will be a tiny bit more efficient as it doesn't have to check the other if's is one passes. So you are checking 1 or 2 statements instead of 3.
One note and this makes it more clear if it's in none of the above then $post_types
is undefined, which probably is not a good thing.
Upvotes: 1