Extending Longview for Image Attachments

The Longview theme has built-in support for displaying attachment images. Image attachment pages are an often neglected feature of WordPress themes. With the extensibility of the Longview theme, adding support for image attachment pages was pretty easy.

Please note: this process can be followed to add attachment page support for other file types as well.

According to the WordPress template hierarchy, the image.php file will automatically be used to display image attachments. All the code in this tutorial will be placed in this file within the theme.

Displaying the Image

function thmfdn_attachment_content( $content ) {
	global $post;

	return wp_get_attachment_image( $post->ID, 'large' );
}
add_filter( 'the_content', 'thmfdn_attachment_content' );

This code uses the the_content filter hook to replace the typical post content with the image attachment file.

Adding a Return Link

function thmfdn_attachment_template() {
	the_post_navigation();
}
add_action( 'thmfdn_entry_content_after', 'thmfdn_attachment_template' );

This function uses the thmfdn_entry_content_after action hook to place a navigation link below the attachment image. This link leads back to the post that the image was attached to.

Loading the Page Template

The only thing left to do is load the page.php template that the image.php file is extending. It can be done with the following code:

require_once( get_stylesheet_directory() . '/page.php' );

This loads the page.php file from a child theme, if present. If the page.php file doesn’t exist in the child theme, then it loads the page.php file from the current theme (Longview).

Please note: You could also set the page layout from this file, but Longview defaults to setting most page layouts from the includes/settings.php file.