Site logo

Archived topic

Link Featured Image in CPT Archive to Custom Field

3 replies · Started by Walter on October 4, 2019

Viewing posts 1–4 of 4

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;
}

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;
}

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;
}

Glad you got this sorted out :)

This archived topic is closed to new replies.