Support » Plugin: Dynamic Featured Image » function to call secondary thumbnail for backstretch script

  • Resolved mac4media

    (@mac4media)


    Hi,

    Is there a simple way of modifying the Backstretch function below to call the secondary featured image/post thumbnail?

    if ( has_post_thumbnail() ) {
    			wp_localize_script( 'backstretch-init', 'BackStretchImg', array( 'src' => wp_get_attachment_url( get_post_thumbnail_id() ) ) );
    		}

    Any assistance would be much appreciated.

    Thanks
    Darrell

    https://wordpress.org/plugins/dynamic-featured-image/

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Ankit Pokhrel

    (@ankitpokhrel)

    Hi @mac2media,

    Try something like this:

    if ( has_post_thumbnail() ) {
    	global $dynamic_featured_image;
    	$second_featured_image = $dynamic_featured_image->get_nth_featured_image(2);
    	if( !empty($second_featured_image) ) {
    		wp_localize_script( 'backstretch-init', 'BackStretchImg', array( 'src' =>  $second_featured_image['full'] ) );
    	}
    }

    Regards,
    Ankit

    Thread Starter mac4media

    (@mac4media)

    Hi Ankit,

    Thanks for this solutions it works almost perfectly. Just one small thing. Is it possible for the above function to default back to the main featured image if a dynamic featured image isn’t available?

    As is, if i don’t upload a second featured image the backstretch block will be blank as it doesn’t default back to the main image.

    Kind regards
    Darrell

    Plugin Author Ankit Pokhrel

    (@ankitpokhrel)

    Hi @mac4media,

    You can put else statement in above code for fallback:

    if ( has_post_thumbnail() ) {
        global $dynamic_featured_image;
        $second_featured_image = $dynamic_featured_image->get_nth_featured_image(2);
        if( !empty($second_featured_image) ) {
            wp_localize_script( 'backstretch-init', 'BackStretchImg', array( 'src' =>  $second_featured_image['full'] ) );
        } else {
            wp_localize_script( 'backstretch-init', 'BackStretchImg', array( 'src' => wp_get_attachment_url( get_post_thumbnail_id() ) ) );
        }
    }

    You can modify above script as per your requirement.

    Regards,
    Ankit

    Thread Starter mac4media

    (@mac4media)

    Hi Ankit,

    Last request i promise 🙂 I’m also trying to display the second featured image i a masonry block. How would i change the snippet below to display the second featured image?

    //* Helper function to display featured image
    function masonry_block_post_image() {
    		$img = genesis_get_image( array( 'format' => 'html', 'size' => 'masonry-brick-image', 'attr' => array( 'class' => 'post-image' ) ) );
    		printf( '<a href="%s" title="%s">%s</a>', get_permalink(), the_title_attribute( 'echo=0' ), $img );
    }

    Kind regards
    Darrell

    Plugin Author Ankit Pokhrel

    (@ankitpokhrel)

    Hi Darrell,

    Try this. It will also fallback to default genesis image if no second image is found. The code below is untested but it should work.

    <?php
    //* Helper function to display featured image
    function masonry_block_post_image() {
    	global $dynamic_featured_image;
    	$second_featured_image = $dynamic_featured_image->get_nth_featured_image(2, get_the_ID());
    	if( !empty($second_featured_image) ) {
    		$img = '<img src="' . $second_featured_image['full'] . '" class="post-image">';
    	} else {
    		$img = genesis_get_image( array( 'format' => 'html', 'size' => 'masonry-brick-image', 'attr' => array( 'class' => 'post-image' ) ) );
    	}
    
    	printf( '<a href="%s" title="%s">%s</a>', get_permalink(), the_title_attribute( 'echo=0' ), $img );
    }

    A plugin review would be a great help 🙂

    Regards,
    Ankit

    Thread Starter mac4media

    (@mac4media)

    Hi Ankit,

    Much appreciate the super fast response, and a defo on a 5 star review.

    I have it working almost perfectly now, the snippet above seem to use the full image

    $img = '<img src="' . $second_featured_image['full'] . '" class="post-image">';

    just wondering if there’s a way of calling the masonry-brick-image?

    add_image_size( 'masonry-brick-image', 390, 390, array( 'center', 'top' ) );

    I’ve tried replacing “full” with “masonry-brick-image” but that doesn’t
    seem to work. any ideas?

    Kind regards
    Darrell

    Plugin Author Ankit Pokhrel

    (@ankitpokhrel)

    Yes there is a way. You can use get_image_url or get_image_thumb to get required image.

    <?php
    //* Helper function to display featured image
    function masonry_block_post_image() {
        global $dynamic_featured_image;
        $second_featured_image = $dynamic_featured_image->get_nth_featured_image(2, get_the_ID());
        $masonryImage = $dynamic_featured_image->get_image_url( $second_featured_image['attachment_id'], "masonry-brick-image" );
        if( !empty($second_featured_image) ) {
            $img = '<img src="' . $masonryImage . '" class="post-image">';
        } else {
            $img = genesis_get_image( array( 'format' => 'html', 'size' => 'masonry-brick-image', 'attr' => array( 'class' => 'post-image' ) ) );
        }
    
        printf( '<a href="%s" title="%s">%s</a>', get_permalink(), the_title_attribute( 'echo=0' ), $img );
    }

    Regards,
    Ankit

    Thread Starter mac4media

    (@mac4media)

    Thanks so much for your support, it works perfectly.

    5 Star plugin and 5+ for the support

    Kind regards
    Darrell

    Thread Starter mac4media

    (@mac4media)

    Hi Ankit,

    Sorry to bother you again 🙂 I have one last error i need to use the second featured image.
    I’ve tried to decipher the snippet provided but as i’m not a coder i’ve had no luck.

    Any chance you could show me how to apply the above to this function? pleeeese 🙂

    function rp4wp_example_my_thumbnail_size( $thumb_size ) {
    	return 'masonry-brick-image';
    }
    
    add_filter( 'rp4wp_thumbnail_size', 'rp4wp_example_my_thumbnail_size' );

    Kind regards
    Darrell

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘function to call secondary thumbnail for backstretch script’ is closed to new replies.