WordPress Code Snippets

A fresh mix of code snippets and tutorials

WordPress link attachment to parent post

277

This tutorial provides four techniques for linking WordPress attachment pages back to their parent posts. Adding such a link makes it easy for users to view an image or other attachment, and then quickly navigate back to the original gallery in the parent post.

WordPress is great because there are so many ways to do things. This can be seen in the labyrinth of functions, templates, and tags that can be used to display WordPress post attachments. For example, the following four techniques that work equally well to display a link from the attachment page that takes the user back to the attachment’s parent post.

Link to parent: Method 1

This technique is nice and simple, but makes a mess in markup because of WordPress’ out-of-control auto-added markup attributes:

<?php the_post_navigation(array('prev_text' => '<div class="nav-parent">'. __('Published in', 'shapespace') .' %title</div>')); ?>

The trick here is using the_post_navigation() to display the parent link. Pop that into your image.php or attachment.php template file and you’re good to go. Of course, you’ll want to edit the text and change the language domain to suit your needs.

Link to parent: Method 2

This technique is very basic, but gives you full control over the output:

<a href="<?php echo get_permalink($post->post_parent); ?>">Link Text</a>

This technique uses get_permalink() to generate the parent link based on the value of $post->post_parent. Fleshing it out a little, we can do something like this:

<?php if ($post->post_parent) {
	$parent_link = get_permalink($post->post_parent); 
	$parent_title = get_the_title($post->post_parent); 
	echo '<a href="'. $parent_link .'">'. $parent_title .'</a>';
} ?>

This works anywhere that the $post object is available, so remember to declare it as a global variable if working outside of the WordPress Loop.

Link to parent: Method 3

Next up, here is a technique that works only for posts with one attachment:

<?php $attachments = get_posts(array('post_type' => 'attachment', 'numberposts' => -1));

foreach ($attachments as $attach) {
	$parent_id    = $attach->post_parent;
	$parent_title = get_the_title($parent_id);
	$parent_link  = get_permalink($parent_id);
	echo '<a href="'. $parent_link .'">'. $parent_title .'</a>';
} ?>

Although limited to posts with a single attachment, this technique is super flexible because it’s based on get_posts(), which can be configured to get just about any set of posts that is required.

Link to parent: Method 4

This probably is the best, most flexible technique. It’s awesome because it uses get_queried_object(), which is a very underrated yet useful function:

<?php
$attachment   = get_queried_object();
$parent_url   = get_permalink($attachment->post_parent);
$parent_title = get_post($attachment->post_parent)->post_title;

echo '<a href="'. $parent_url .'">'. $parent_title .'</a>';
?>

This is my preferred technique for implementing parent links in attachment pages. You can see it in action in the following shapeSpace function:

// shapeSpace : get attachment parent post link
function shapeSpace_get_attachment_parent_link() {
	
	if (!is_attachment()) return false;
	
	$attachment   = get_queried_object();
	$parent_url   = get_permalink($attachment->post_parent);
	$parent_title = get_post($attachment->post_parent)->post_title;
	
	$link = '<a href="'. $parent_url .'">'. __('Attached to: ', 'shapespace') . $parent_title .'</a>';
	
	return $link;
	
}

This function is included in shapeSpace via /inc/functions-attachments.php, and is called via the attachment.php theme template. To see it in action, you can download my shapeSpace theme template.

Learn more

WordPress Themes In Depth


Show Support

Like our new Facebook Page to show support!