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.

I use the following code in functions.php in order to obtain all the images attached to a post so I can retrieve them via AJAX.

<?php
function get_all_images($post_id=0, $size='bigger', $attributes='') {
    $post_id = $_POST['post_id'];
    if ($post_id<1) $postid = get_the_ID();
    if ($images = get_children(array(
        'post_parent' => $post_id,
        'post_type' => 'attachment',
        'numberposts' => -1,
        'orderby' => 'menu_order',
        'post_mime_type' => 'image',)))
        $response = '';
        foreach($images as $image) {
            $attachment=wp_get_attachment_image_src($image->ID, $size);
            $tranparent=get_template_directory_uri();
            $response .= '<img src="'.$tranparent.'/images/transparent.gif" data-original="'.$attachment[0].'" alt="" class="imarge" />';
        }
    die( json_encode( $response ) );
}?>

What I would like to do is to add a <h2 class="titles">Title of the post</h2> in the response before all the images.

So I tried this:

<?php
function get_all_images($post_id=0, $size='bigger', $attributes='') {
    $post_id = $_POST['post_id'];
    if ($post_id<1) $postid = get_the_ID();
    if ($images = get_children(array(
        'post_parent' => $post_id,
        'post_type' => 'attachment',
        'numberposts' => -1,
        'orderby' => 'menu_order',
        'post_mime_type' => 'image',)))
        $img = '';
        $title = get_the_title($post_id);
        $response = '<h2 class="titles">'.$title.'</h2>'.$img.'';
        foreach($images as $image) {
            $attachment=wp_get_attachment_image_src($image->ID, $size);
            $tranparent=get_template_directory_uri();
            $img .= '<img src="'.$tranparent.'/images/transparent.gif" data-original="'.$attachment[0].'" alt="" class="imarge" />';
        }
    die( json_encode( $response ) );
}?>

I now get the post's title but not the images anymore. Any idea?

share|improve this question

closed as too localized by Brian Fegter, kaiser, Wyck, Rarst Oct 2 '12 at 21:07

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Change $img to $response –  Brian Fegter Sep 13 '12 at 14:32
    
This is a simple error with variable name confusion. Close voting as too-localized. –  Brian Fegter Sep 13 '12 at 14:33
add comment

2 Answers 2

up vote 0 down vote accepted

You want to add the title to the response, not replace the response with the title. Like this:

<?php
function get_all_images($post_id=0, $size='bigger', $attributes='') {
    $post_id = $_POST['post_id'];
    if ($post_id<1) $postid = get_the_ID();
    if ($images = get_children(array(
        'post_parent' => $post_id,
        'post_type' => 'attachment',
        'numberposts' => -1,
        'orderby' => 'menu_order',
        'post_mime_type' => 'image',)))
            $title = get_the_title($post_id);
        $response = '<h2 class="titles">'.$title.'</h2>';
        foreach($images as $image) {
            $attachment=wp_get_attachment_image_src($image->ID, $size);
            $tranparent=get_template_directory_uri();

            $response .= '<img src="'.$tranparent.'/images/transparent.gif" data-original="'.$attachment[0].'" alt="" class="imarge" />';
        }
    die( json_encode( $response ) );
}?>

Update

Corrected to add it before the loop.

share|improve this answer
    
This produce the title to be added several times if there are several images attached to the post. I wanted the title to appear once per post. I added the solution. Many thanks for your help. –  Gab Sep 13 '12 at 14:34
    
I noticed the mistake, and fixed it, a moment after I posted the answer. Glad you had figured it out, though. –  marfarma Sep 13 '12 at 14:36
add comment
<?php
function get_all_images($post_id=0, $size='bigger', $attributes='') {
    $post_id = $_POST['post_id'];
    if ($post_id<1) $postid = get_the_ID();
    if ($images = get_children(array(
        'post_parent' => $post_id,
        'post_type' => 'attachment',
        'numberposts' => -1,
        'orderby' => 'menu_order',
        'post_mime_type' => 'image',)))
        $img = '';
        $title = get_the_title($post_id);
        $response = '<h2 class="titles">'.$title.'</h2>';
        foreach($images as $image) {
            $attachment=wp_get_attachment_image_src($image->ID, $size);
            $tranparent=get_template_directory_uri();
            $response .= '<img src="'.$tranparent.'/images/transparent.gif" data-original="'.$attachment[0].'" alt="" class="imarge" />';
        }
    die( json_encode( $response ) );
}?>
share|improve this answer
add comment

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