• Resolved Jack

    (@moxie)


    I’m sorry, this title is probably complete nonsense, but I’m trying to get the second featured image to be the header image in a backstretch script inside a Genesis theme.

    		$image = array( 'src' => has_post_thumbnail() ? genesis_get_image( array( 'format' => 'url' ) ) : '' );
    		wp_localize_script( 'monochrome-backstretch-set', 'BackStretchImg', $image );

    So you see, output of the code

    kdmfi_the_featured_image( 'featured-image-2', 'full' );

    should be inside the variable $image, but I have no idea how to do that.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Marcus Kober

    (@marcuskober)

    Hi there,

    try this one:

    $image = '';
    if (has_post_thumbnail('featured-image-2')) {
      $image = kdmfi_get_the_featured_image('featured-image-2');
    }
    wp_localize_script( 'monochrome-backstretch-set', 'BackStretchImg', $image );

    kdmfi_the_featured_image outputs the image whilst kdmfi_get_the_featured_image returns it for use in a variable.

    See the function list in the FAQ section of the plugin: https://ww.wp.xz.cn/plugins/multiple-featured-images/#description

    Regards,
    Marcus

    Thread Starter Jack

    (@moxie)

    Thanks Marcus for the suggestion.
    It is not working yet, so I added the complete code of that php page.
    The post I’m testing this with has two featured images. One is the default WordPress featured image.

    // Add body class if post has featured image.
    add_filter( 'body_class', 'monochrome_body_class_post' );
    function monochrome_body_class_post( $classes ) {
    
    	if ( has_post_thumbnail() ) {
    		$classes[] = 'featured-image';
    	}
    
    	return $classes;
    
    }
    
    // Enqueue Backestretch scripts.
    add_action( 'wp_enqueue_scripts', 'monochrome_enqueue_backstretch_post' );
    function monochrome_enqueue_backstretch_post() {
    
    	if ( has_post_thumbnail() ) {
    
    		wp_register_script( 'monochrome-backstretch', get_stylesheet_directory_uri() . '/js/backstretch.js', array( 'jquery' ), '1.0.0', true );
    		wp_register_script( 'monochrome-backstretch-set', get_stylesheet_directory_uri() . '/js/backstretch-set.js', array( 'jquery', 'monochrome-backstretch' ), '1.0.0', true );
    	}
    }
    
    // Run functions if post has featured image and full-width content layout.
    add_action( 'genesis_before', 'monochrome_setup_full_width' );
    function monochrome_setup_full_width() {
    
    	$run = genesis_site_layout() === 'full-width-content' && has_post_thumbnail();
    
    	if ( ! $run ) {
    		return;
    	}
    
    	// Localize Backstretch script.
    	add_action( 'genesis_after', 'monochrome_set_background_image_post' );
    	function monochrome_set_background_image_post() {
    
    		wp_enqueue_script( 'monochrome-backstretch' );
    		wp_enqueue_script( 'monochrome-backstretch-set' );
    		
    		$image = '';
    		if (has_post_thumbnail()) {
      		$image = kdmfi_get_the_featured_image('featured-image-2');
    		}
    		wp_localize_script( 'monochrome-backstretch-set', 'BackStretchImg', $image );
    
    	}
    
    	// Hook entry background area.
    	add_action( 'genesis_after_header', 'monochrome_entry_background_post' );
    	function monochrome_entry_background_post() {
    
    		echo '<div class="entry-background"></div>';
    
    	}
    Plugin Author Marcus Kober

    (@marcuskober)

    Sorry, there’s an error in my code.

    Please use this one:

    $image = '';
    if (kdmfi_has_featured_image('featured-image-2')) {
      $image = kdmfi_get_the_featured_image('featured-image-2');
    }
    wp_localize_script( 'monochrome-backstretch-set', 'BackStretchImg', $image );

    I accidentially used has_featured_image instead of kdmfi_has_featured_image.

    Please understand that I can’t help you with the full code as it’s not related to my plugin and my time is limited as my plugin is free and I do all development and support in my spare time…

    Regards,
    Marcus

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘How to ‘attach’ the php code to a variable’ is closed to new replies.