Wordpress - Including page title in 'wp get attachment image'

Okay, I've set up a bit of code which searching for all the pages which are a child of the ID 8, then outputs all the attachments (in the gallery) of these pages as unordered list items. You can see the effect so far here http://goo.gl/eq4UF.

The problem I'm having is that I need to include the title of each page before each so you can easily identify which images come below which page. Normally I would just add this in, but the list items al use masonry and are positioned all over the page using some JS so they never appear beside the first image in the list.

I therefore will add the title of the page to every <li> in the <ul> which will allow the title to run with each image but I don't know how to include this in the wp get attachment image function. Both the_title and wp_title doesn't work inside this loop. apply_filters( 'the_title', $attachment->post_title ); obviously takes the image title, but is there any good to take the page title?

Thanks in advance and hope this made sense, R

<?php $postslist = get_pages('number=9999&sort_order=DESC&sort_column=post_date&child_of=8');
foreach ($postslist as $post) :
setup_postdata($post); ?>
<ul class="main-projects-list">
<?php

$args = array(
   'post_type' => 'attachment',
   'numberposts' => -1,
   'post_status' => null,
   'post_parent' => $post->ID,
   'orderby' => 'menu_order',
   'order' => 'ASC',
  );

  $attachments = get_posts( $args );
     if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
           echo '<li class="each-image">';
           echo wp_get_attachment_image( $attachment->ID, 'large' );
           echo '<p>';
           echo apply_filters( 'the_title', $attachment->post_title );
           echo '</p></li>';
          }
     }

?>
</ul>
<?php endforeach; ?>

Answers


You can try this:

<?php $postslist = get_pages('number=9999&sort_order=DESC&sort_column=post_date&child_of=8');
foreach ($postslist as $post) :
setup_postdata($post); ?>
<ul class="main-projects-list">
<?php

$args = array(
   'post_type' => 'attachment',
   'numberposts' => -1,
   'post_status' => null,
   'post_parent' => $post->ID,
   'orderby' => 'menu_order',
   'order' => 'ASC',
  );

  $attachments = get_posts( $args );
     if ( $attachments ) {
        $post_title = get_the_title($post->ID); // We get the post title
        foreach ( $attachments as $attachment ) {
           $img_title = apply_filters( 'the_title', $post_title . ' - ' . $attachment->post_title ); // We create the image title with the 2 strings
           echo '<li class="each-image">';
           echo wp_get_attachment_image( $attachment->ID, 'large' , false, array('title' => $img_title));
           echo '<p>';
           echo $img_title;
           echo '</p></li>';
          }
     }

?>
</ul>
<?php endforeach; ?>

Need Your Help

Looking for an example of fb:tabs and AJAX

php ajax facebook fbjs

I want to use http://wiki.developers.facebook.com/index.php/Fb:tabs and have the tabs load/change dynamically using AJAX. Are there any examples online or would anyone be kind enough to post an exa...

Python: MySQLdb working great on Linux, not Windows

python linux windows mysql-python

I have a problem with an application I am writing.