Goal: From my image.php, I am displaying the attachment image and some descriptions. I also want get the post from where it was originally posted. I only need the title and the url so that you can link back from the original post.
Problem: It's not displaying the correct post title and url. Instead, it's displaying the first and last post I posted. When I change the number to "1" instead of "-1", the name just change to either the first post or the last post.
Code: From this post: Find the post an attachment is attached to
<?php
// Get all image attachments
$all_images = get_posts( array(
'post_type' => 'attachment',
'numberposts' => -1,
) );
// Step through all image attachments
foreach ( $all_images as $image ) {
// Get the parent post ID
$parent_id = $image->post_parent;
// Get the parent post Title
$parent_title = get_the_title( $parent_id );
// Get the parent post permalink
$parent_permalink = get_permalink( $parent_id );
}
echo "This image is posted to: <a href='".$parent_permalink ."' >".$parent_title."</a>";
?>