WordPress.org

Ready to get started?Download WordPress

Forums

Display Excerpts with Thumbnails for Child Pages of Current Page (7 posts)

  1. Chris
    Member
    Posted 2 years ago #

    I am trying to find a nice snippet to display the excerpts of the current pages children.

    I found this bit of code but am not sure how to also incorporate the featured image (thumbnail) into it.

    <?php
    $pageChildren = get_pages('sort_column=menu_order&hierarchical=0&child_of='.$post->ID);
    if ( $pageChildren ) {
    foreach ( $pageChildren as $pageChild ) {
    echo '<p>And the title is: '. $pageChild->post_title.'</p>';
    if ($pageChild->post_excerpt){
    echo '<p>And the excerpt is: '.$pageChild->post_excerpt.'</p>';
    }
    }
    }
    ?>

    Any suggestions would be great!

  2. alchymyth
    Forum Moderator
    Posted 2 years ago #

  3. Chris
    Member
    Posted 2 years ago #

    OK, so I almost got this bad boy dialed in. Here is my current code:

    <?php
    $pageChildren = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'menu_order', 'hierarchical' => '0' ) );
    if ( $pageChildren ) {
    foreach ( $pageChildren as $pageChild ) {
    echo '<h2>ID) . '" title="' . $pageChild->post_title . '">'. get_the_post_thumbnail($pageChild->ID).'</h2>';
    if ($pageChild->post_excerpt){
    echo '<p>'.$pageChild->post_excerpt.'</p>';
    }
    }
    }
    ?>

    But I want to also have it pull a custom field. So something like this:

    <?php
    $pageChildren = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'menu_order', 'hierarchical' => '0' ) );
    if ( $pageChildren ) {
    foreach ( $pageChildren as $pageChild ) {
    echo '<h2>ID) . '" title="' . $pageChild->post_title . '">'. get_the_post_thumbnail($pageChild->ID).'</h2>';
    if ($pageChild->post_excerpt){
    echo '<p>'.$pageChild->post_excerpt.'</p>';
    }
    echo get_post_meta($post->ID, 'website-link', true);
    }
    }
    ?>

    But I cannot seem to get it to pull the custom field. Any suggestions?

  4. alchymyth
    Forum Moderator
    Posted 2 years ago #

    But I want to also have it pull a custom field.

    custom field of the current page or of each child page?

  5. Chris
    Member
    Posted 2 years ago #

    I would like to have it show a custom field of each child page.

  6. Chris
    Member
    Posted 2 years ago #

    Figured it out on my own for anyone trying to do this.

    The following will call the excerpt of the children of the current page, the thumbnail of the children of the current page and the custom field of the children of the current page.

    <?php
    $pageChildren =  get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'menu_order', 'hierarchical' => '0' ) );
    if ( $pageChildren ) {
      foreach ( $pageChildren as $pageChild ) {
        echo '<h2><a>ID) . '" title="' . $pageChild->post_title . '">'. get_the_post_thumbnail($pageChild->ID).'</a></h2>';
        if ($pageChild->post_excerpt){
          echo '<p>'.$pageChild->post_excerpt.'</p>';
        }
                    echo get_post_meta($pageChild->ID, 'custom-field-name', true);
      }
    }?>

    [Please post code or markup snippets between backticks or use the code button.]

  7. jamieb7210
    Member
    Posted 2 years ago #

    Hi Chris, can you doublecheck that code? Some of the syntax is wrong I believe in the echo?

Topic Closed

This topic has been closed to new replies.

About this Topic