Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

So I found this post: Find the post an attachment is attached to and implemented Chip's suggestion but having a weird outcome. I am trying to pull the title of the post that the specific image is attached to. This is my query:

<?php
    $args = array(
        'post_type' => 'attachment',
        'posts_per_page' => -1,
        'post_status' => 'any',
        'post_parent' => null,
        'tax_query' => array(
            array(
                'taxonomy' => 'post_tag',
                'field' => 'slug',
                'terms' => 'logo'
            )
        )
    );

    $attachments = get_posts( $args );
    foreach ( $attachments 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 );
    }


    if ( $attachments ) {
        foreach ( $attachments as $post ) {

            $logoimg = wp_get_attachment_image($post->ID, 'Work Gallery');

This is the line where I am calling the title: echo ' ' . $parent_title . ' ';

This kind of works but it is outputting 1 post title in particular and for all the images on the page. Any thoughts as to why its not dynamically picking up each images associated post title?

Thanks!

share|improve this question
    
What are you trying to get? The image's title? Or the title of the page it is attached to? The question at one point sounds like you want the later but at another point sounds like you are complaining that you are getting the later. –  s_ha_dum Sep 25 '13 at 1:32
add comment

1 Answer

up vote 2 down vote accepted

This kind of works but it is outputting 1 post title in particular and for all the images on the page. Any thoughts as to why its not dynamically picking up each images associated post title?

$attachments = get_posts( $args );
foreach ( $attachments 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 );
}

This code loops over each image. Let's debug it by hand.

As an example, let's assume there are 3 images.

The first image has a parent ID of 1, a title of "Foo" and link to that post,
The second image has a parent ID of 2, a title of "Bar" and link to that post,
The third image has a parent ID of 3, a title of "Baz" and link to that post,

So, the first time through the loop:

$parent_id is set to 1,
$parent_title is set to Foo,
$parent_permalink is set to the link to Foo.

The second time through the loop:

$parent_id is set to 2,
$parent_title is set to Bar,
$parent_permalink is set to the link to Bar.

All the data for the post "Foo" is overwritten with the data for post "Bar". Why? Because the code does not save that other data before writing the new data to $parent_id, $parent_title and $parent_permalink.

On the third trip through the loop:

$parent_id is set to 3,
$parent_title is set to Baz,
$parent_permalink is set to the link to Baz.

All the data for the post "Bar" is overwritten with the data for post "Baz".

When this loop is done, the $parent_id, $parent_title and $parent_permalink variables only hold whatever data is in the last item of the $attachments array. So, the reason that only the last parent post is being used is because that is what the code told PHP to do. The code says, "Throw away those other values."

What you probably want to do is something like this:

$images = get_posts( $args );

if ( $images ) {

    foreach ( $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 );

        $logoimg = wp_get_attachment_image( $image->ID, 'Work Gallery' );
share|improve this answer
    
thank you for the very detailed & thorough explanation, now my images are pulling the title from the Post that they are assigned to. I guess I have one last question on this. I use this snippet on another page: data-type="<?php $posttags = get_the_tags(); if ($posttags) { foreach ($posttags as $tag) { echo str_replace('-','_',$tag->slug . ' '); } } ?> <?php $posttags = get_the_terms($post->ID, 'type'); if ($posttags) { foreach($posttags as $tag) { echo str_replace('-','_',$tag->slug . ''); } } ?> to get the tags from the post. Anyway to do this for these images attached post? –  Danny Sep 25 '13 at 13:46
    
You're welcome and you should open another question for this new problem. –  Charles Clarkson Sep 25 '13 at 17:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.