Support » Fixing WordPress » Display Image from Sub Page

  • Hi All

    If you visit this page hopefully you can see what I am trying to do:

    I have displayed the list of sub pages using:

      global $id;
      &date_format=$date_format"); ?>

    However, I would like to display the first image from each page. Is this possible please?



Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator keesiemeijer


    Try with something like this:

    global $id;
    $mypages = get_pages('child_of='.$id);
    foreach ( $mypages as $mypage  ) {
    $attachments = get_children(array('post_parent' => $mypage->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order'));
    if ( ! is_array($attachments) ) continue;
    $count = count($attachments);
    $first_attachment = array_shift($attachments);
    echo wp_get_attachment_image($first_attachment->ID);

    Thanks for the code but it isn’t showing anything!

    Do I need to insert anything in the code please?


    Moderator keesiemeijer


    try this: $mypages = get_pages('child_of='.$posts[0]->ID);
    or this: $mypages = get_pages('child_of=306');

    The images have to be attached to the Page (check gallery). You have to upload them when editing the page so they get attached to that Page otherwhise the images will not show because they are not an attachement of that Page.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Display Image from Sub Page’ is closed to new replies.