0

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;

}
3
  • 1
    Personally I would use something like: $post_arr=array_intersect(array('post', 'page'), get_theme_mod( 'auto_add_featured_image', array( 'post', 'page' )) ); along with if (!empty($post_arr) && is_singular($post_arr)) {.... Commented Sep 6, 2017 at 20:55
  • 1
    You'd need to delete empty($show) but that case is covered with !empty($post_arr). Commented Sep 6, 2017 at 21:00
  • I'm going to give yours a whirl. Thanks! Commented Sep 6, 2017 at 21:06

2 Answers 2

1

The following should be sufficient:

$post_types = is_singular( array_intersect( array ( 'post', 'page' ), $show ) );
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you sooooo much. That is significantly better. I had never see this before and wouldn't have come up with it in a zillion years.
1

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.

1 Comment

Thanks! I will give it a spin and come back to select. I like that it's more efficient.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.