Link the Media file to the post which it is attached to

Content Views Pro helps you to show Media easily.
By default, media title, thumbnail and Read More button will link to itself. To link to the post which it is attached to, please add this code to file functions.php of your active theme:

// Content Views Pro - Media link to post which it is attached to
add_filter( 'pt_cv_field_href', 'cvp_theme_media_link_post', 100, 2 );
function cvp_theme_media_link_post( $href, $post ) {
	global $pt_cv_id;
	if ( $pt_cv_id === 'VIEW_ID' ) {
		if ( !empty( $post->post_parent ) ) {
			$href = get_permalink( $post->post_parent );
		}
	}

	return $href;
}

(Please replace VIEW_ID with ID of your View)

If you want to keep the media title, thumbnail links to itself, but links the Read More button to the post, please add this code to file functions.php of your active theme:

// Content Views Pro - Media "Read More" link to post which it is attached to
add_filter( 'pt_cv_field_href', 'cvp_theme_media_readmore_link_post', 100, 2 );
function cvp_theme_media_readmore_link_post( $href, $post ) {
	global $pt_cv_id;
	if ( $pt_cv_id === 'VIEW_ID' ) {
		$trace = debug_backtrace();

		if ( !empty( $trace[ 5 ][ 'function' ] ) && $trace[ 5 ][ 'function' ] === '_field_content' && !empty( $post->post_parent ) ) {
			$href = get_permalink( $post->post_parent );
		}
	}

	return $href;
}

(Please replace VIEW_ID with ID of your View)

Best regards,

Scroll to Top