[Resolved] Link Featured Image in CPT Archive to Custom Field

Home Forums Support Link Featured Image in CPT Archive to Custom Field

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1026606
    Walter

    Hello,

    First of all I love the support you provide Tom and LOOVE Generate Press. So here’s my dilemma. I used a code similar to what you provided in this thread.

    With the code below, it applies the function to ALL my archive pages instead of just e_editions. I can’t figure out what I’m doing wrong but I’m no php expert, just no enough to tinker and make adjustments.

    The idea is to have the featured image on the e_editions cpt link to the acf field url I’ve created (get_field(‘download_file’)).

    Note: I created single-e_editions.php, archive-e_editions.php, and content-E_Editions.php for the CPT. The archive-e_editions.php has this code: get_template_part( ‘content-E-Editions’, get_post_format() ); Maybe I messed something up there? Any help would be much appreciated.

    //Give me styling control over the featured image thumbs in cpt
    add_filter( ‘generate_single_featured_image_output’, ‘tu_change_cpt_image_structure’ );
    add_filter( ‘generate_featured_image_output’, ‘tu_change_cpt_image_structure’ );
    function tu_change_cpt_image_structure( $output ) {
    if ( is_archive(‘e_editions’) ) {
    $output = sprintf( // WPCS: XSS ok.


    %2$s

    ‘,
    esc_url( get_field(‘download_file’) ),
    get_the_post_thumbnail(
    get_the_ID(),
    apply_filters( ‘generate_page_header_default_size’, ‘full’ ),
    array(
    ‘itemprop’ => ‘image’,
    )
    )
    );
    }

    echo $output;
    }

    #1026612
    Walter

    Sorry I didn’t put snippet into code brackets. Here you go.

    //Give me styling control over the featured image thumbs in cpt
    add_filter( 'generate_single_featured_image_output', 'tu_change_cpt_image_structure' );
    add_filter( 'generate_featured_image_output', 'tu_change_cpt_image_structure' );
    function tu_change_cpt_image_structure( $output ) {
    	if ( is_archive('e_editions') ) {
    		$output = sprintf( // WPCS: XSS ok.
    			'<div class="post-image">
    				<a href="%1$s">
    					%2$s
    				</a>
    			</div>',
    			esc_url( get_field('download_file') ),
    			get_the_post_thumbnail(
    				get_the_ID(),
    				apply_filters( 'generate_page_header_default_size', 'full' ),
    				array(
    					'itemprop' => 'image',
    				)
    			)
    		);
    	}	
    
    	echo $output;
    }
    #1026618
    Walter

    UPDATE:I figured it out! Thank you anyways! Here what I did in case anyone has the same need:

    //Give me styling control over the featured image thumbs in cpt
    add_filter( 'generate_single_featured_image_output', 'tu_change_cpt_image_structure' );
    add_filter( 'generate_featured_image_output', 'tu_change_cpt_image_structure' );
    function tu_change_cpt_image_structure( $output ) {
    	if ( is_post_type_archive('e_editions') ) {
    		$output = sprintf( // WPCS: XSS ok.
    			'<div class="post-image">
    				<a href="%1$s">
    					%2$s
    				</a>
    			</div>',
    			esc_url( get_field('download_file') ),
    			get_the_post_thumbnail(
    				get_the_ID(),
    				apply_filters( 'generate_page_header_default_size', 'full' ),
    				array(
    					'itemprop' => 'image',
    				)
    			)
    		);
    	}	
    
    	echo $output;
    }
    #1026623
    Tom
    Lead Developer
    Lead Developer

    Glad you got this sorted out 🙂

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.