logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$10
excerpts and thumbnail captions for child pages

On a custom page template I have the following code:

<?php $pages = get_pages('child_of='.$post->ID.'&parent='.$post->ID.'&sort_column=menu_order&sort_order=ASC'); ?>
<?php foreach ($pages as $page): ?>
<?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>
<?php /* 1) Add a new conditional statement here to check if the thumbnail has a caption, and if it has, print it */ ?>
<?php echo $page->post_title; ?>
<?php echo $page->post_excerpt; /* 2) Currently, if the excerpt metabox is empty, nothing is printed.
I would like this code re-worked so a snippet from the post_content is printed if the post_excerpt is empty. A 'More' link should be added if required */?>
<?php endforeach; ?>

This page template code loops through all immediate child pages of the current parent page and outputs a thumbnail, title and excerpt for any immediate child pages that are found.

Please can someone update my code to: 1) include a conditional statement to check for and print image captions; and 2) re-work my echo $page->post_excerpt; statement so a text snippet is displayed even if no manual except has been provided. This text snippet should contain a 'More' link to the full page.

Suggestions on how to make my entire child page query/loop as efficient as possible, much appreciated.

Please note: I previously tackled this problem using the below query_posts statement.

query_posts( 'post_type=page&orderby=menu_order&order=ASC&post_parent='.$post->ID );

I switched to get_pages after reading that query_posts should be avoided if at all possible when creating loops.

Thank you

This question has been answered.

designbuildtest | 06/24/12 at 7:31pm Edit

Previous versions of this question: 06/24/12 at 8:05pm | 06/24/12 at 8:13pm

(3) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    06/24/12
    7:45pm
    Albert Shala says:

    Try this:

    <?php $pages = get_pages('child_of='.$post->ID.'&parent='.$post->ID.'&sort_column=menu_order&sort_order=ASC'); ?>

    <?php foreach ($pages as $page): ?>

    $thumbnail_id = get_post_thumbnail_id($post->ID);
    $thumbnail_image = get_posts(array('p' => $thumbnail_id, 'post_type' => 'attachment'));

    if ($thumbnail_image && isset($thumbnail_image[0])) {
    echo '<span>'.$thumbnail_image[0]->post_excerpt.'</span>';
    }

    <?php the_title(); ?>

    <?php if (isset($page->post_excerpt){
    echo $page;
    }else {
    the_content();
    }
    <?php endforeach; ?>

    Previous versions of this answer: 06/24/12 at 7:45pm

    • 06/24/12 7:40pm

      Albert Shala says:

      <?php $pages = get_pages('child_of='.$post->ID.'&parent='.$post->ID.'&sort_column=menu_order&sort_order=ASC'); ?>

      <?php foreach ($pages as $page): ?>

      <?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>

      <?php /* 1) Add a new conditional statement here to check if the thumbnail has a caption, and if it has, print it */ ?>

      <?php echo $page->post_title; ?>

      <?php if (isset($page->post_excerpt){
      echo $page;
      }else {
      the_content();

      }

      <?php endforeach; ?>

    • 06/24/12 7:40pm

      Albert Shala says:

      <?php $pages = get_pages('child_of='.$post->ID.'&parent='.$post->ID.'&sort_column=menu_order&sort_order=ASC'); ?>

      <?php foreach ($pages as $page): ?>

      <?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>

      <?php /* 1) Add a new conditional statement here to check if the thumbnail has a caption, and if it has, print it */ ?>

      <?php echo $page->post_title; ?>

      <?php if (isset($page->post_excerpt){
      echo $page;
      }else {
      the_content();

      }

      <?php endforeach; ?>

    • 06/24/12 8:01pm

      designbuildtest says:

      Thanks Albert, but this code doesn't work unfortunately.

    • 06/24/12 8:11pm

      Albert Shala says:

      Sorry check my first reply I don't know why i have three replies.

    • 06/24/12 8:16pm

      designbuildtest says:

      Hi Albert, I did try the code from your first reply. This code did not work unfortunately.

    • 06/24/12 10:20pm

      Albert Shala says:

      Do you see an error or are you not getting the desired results?

  • avatar
    Last edited:
    06/24/12
    9:39pm
    Daniel Yoen says:

    Try this :

    <?php
    $pages = get_pages('child_of='.$post->ID.'&parent='.$post->ID.'&sort_column=menu_order&sort_order=ASC');

    foreach ($pages as $page):
    echo get_the_post_thumbnail($page->ID, 'content-images');
    if (get_post(get_post_thumbnail_id())->post_excerpt)
    {
    echo '<div class="caption">' . get_post(get_post_thumbnail_id())->post_excerpt . '</div>';
    }
    echo $page->post_title;
    if ($page->post_excerpt)
    {
    echo $page->post_excerpt;
    }
    else
    {
    global $more;$more = 0;
    the_content('Readmore');
    $more = true;
    }
    endforeach;
    ?>

    • 06/24/12 10:18pm

      designbuildtest says:

      Thanks Daniel. The code doesn't work unfortunately.
      Only one child page is being outputted, the thumbnail caption does not print and the child page excerpt statement is pulling in the parent page excerpt.

  • avatar
    Last edited:
    06/24/12
    10:37pm
    Arnav Joy says:

    try this

    <?php $pages = get_pages('child_of='.$post->ID.'&parent='.$post->ID.'&sort_column=menu_order&sort_order=ASC'); ?>

    <?php foreach ($pages as $page): ?>

    <?php $post_thumbnail_id = get_post_thumbnail_id( $page->ID ); ?>

    <?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>

    <?php $thumbnailDetail = get_post($post_thumbnail_id);?>

    <?php if(!empty($thumbnailDetail->post_excerpt)) echo $thumbnailDetail->post_excerpt;?>

    <?php /* 1) Add a new conditional statement here to check if the thumbnail has a caption, and if it has, print it */ ?>

    <?php echo $page->post_title; ?>

    <?php
    if(!empty($page->post_excerpt))
    echo $page->post_excerpt;
    else{
    echo substr($page->post_content,0,100)."<br>";
    echo '<a href="'.get_permalink($page->ID).'" >More</a>';
    }
    /* 2) Currently, if the excerpt metabox is empty, nothing is printed.

    I would like this code re-worked so a snippet from the post_content is printed if the post_excerpt is empty. A 'More' link should be added if required */?>

    <?php endforeach; ?>

    • 06/24/12 11:04pm

      designbuildtest says:

      This works great - thanks Arnav. One slightly odd piece of behaviour however - if a child page has no thumbnail, the excerpt from the parent page is printed as the image caption. No caption should be printed if a thumbnail doesn't exist. Please could you tweak your code? Thanks.

    • 06/24/12 11:17pm

      designbuildtest says:

      Sorry, also, if no manual excerpt is provided and there is less than 100 characters in 'the_content', the 'more' permalink link is printed when it's not actually needed (i.e. there is no more page content that actually needs to be displayed). Thanks.

    • 06/25/12 12:12am

      Arnav Joy says:

      try this

      <?php $pages = get_pages('child_of='.$post->ID.'&parent='.$post->ID.'&sort_column=menu_order&sort_order=ASC'); ?>



      <?php foreach ($pages as $page): ?>

      <?php if(has_post_thumbnail($page->ID)) { ?>

      <?php $post_thumbnail_id = get_post_thumbnail_id( $page->ID ); ?>



      <?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>



      <?php $thumbnailDetail = get_post($post_thumbnail_id);?>



      <?php if(!empty($thumbnailDetail->post_excerpt)) echo $thumbnailDetail->post_excerpt;?>

      <?php } ?>

      <?php /* 1) Add a new conditional statement here to check if the thumbnail has a caption, and if it has, print it */ ?>



      <?php echo $page->post_title; ?>



      <?php

      if(!empty($page->post_excerpt))

      echo $page->post_excerpt;

      else{
      if(strlen($page->post_content) > 100 ) {
      echo substr($page->post_content,0,100)."<br>";
      echo '<a href="'.get_permalink($page->ID).'" >More</a>';
      }
      else
      echo $page->post_content;
      }

      /* 2) Currently, if the excerpt metabox is empty, nothing is printed.



      I would like this code re-worked so a snippet from the post_content is printed if the post_excerpt is empty. A 'More' link should be added if required */?>



      <?php endforeach; ?>


      I will appreciate if you can increase some amount for this question

    • 06/25/12 1:21am

      designbuildtest says:

      Thanks Arnav - that did the trick. There's still a slight quirk when HTML tags in the post_content are counted as characters in the 100 figure, but I'll figure that one out. Thanks again for your help.

    • 06/25/12 1:23am

      Arnav Joy says:

      you can use strip_tags() function for it.
      see manual http://php.net/manual/en/function.strip-tags.php

      Please do not forget to vote.

      Thanks
      Arnav

This question has expired.



designbuildtest, Francisco Javier Carazo Gil voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.