WordPress.org

Ready to get started?Download WordPress

Forums

Display Image from Sub Page (4 posts)

  1. Cregy
    Member
    Posted 3 years ago #

    Hi All

    If you visit this page hopefully you can see what I am trying to do:
    http://weddingcompanycornwall.co.uk/totally-tiara/our-tiaras/

    I have displayed the list of sub pages using:

    <ul>
      <?php
      global $id;
      wp_list_pages("title_li=&child_of=$id&show_date=modified
      &date_format=$date_format"); ?>
    </ul>

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

    Thanks

    Rich

  2. keesiemeijer
    moderator
    Posted 3 years ago #

    Try with something like this:

    <?php
    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);
    }
    ?>
  3. Cregy
    Member
    Posted 3 years ago #

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

    Do I need to insert anything in the code please?

    Thanks

  4. keesiemeijer
    moderator
    Posted 3 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic