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.
<strong>Please note</strong>: 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
Arnav Joy answers:
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; ?>
designbuildtest comments:
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.
designbuildtest comments:
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.
Arnav Joy comments:
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
designbuildtest comments:
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.
Arnav Joy comments:
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
Albert Shala answers:
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; ?>
Albert Shala comments:
<?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; ?>
Albert Shala comments:
<?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; ?>
designbuildtest comments:
Thanks Albert, but this code doesn't work unfortunately.
Albert Shala comments:
Sorry check my first reply I don't know why i have three replies.
designbuildtest comments:
Hi Albert, I did try the code from your first reply. This code did not work unfortunately.
Albert Shala comments:
Do you see an error or are you not getting the desired results?
Daniel Yoen answers:
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;
?>
designbuildtest comments:
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.