Here is the code I’m using:
<?php get_header(); ?>
<div id="content">
<h2><?php the_title(); ?></h2>
<ul title="<?php the_title(); ?>">
<?php $pages = get_pages('child_of='.$post->ID.'&sort_column=post_name&sort_order=ASC&parent='.$post->ID); $count = 0; foreach($pages as $page) { $content = $page->the_content; ?>
<li class="item">
<a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a>
<p><?php echo get_first_image() ?>
</li>
<?php } ?>
And the function:
function get_first_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg";
}
return $first_img;
}
It only seems to display the default image.
I’ve found other code that might be able to help, but I’m still trying to make it work.
https://chrisschuld.com/2009/11/removing-everything-but-images-in-a-wordpress-post/
Here is the original code:
<h2><?php the_title(); ?></h2>
<ul title="<?php the_title(); ?>">
<?php $pages = get_pages('child_of='.$post->ID.'&sort_column=post_name&sort_order=ASC&parent='.$post->ID); $count = 0; foreach($pages as $page) { $content = $page->the_content; ?>
<li class="item">
<a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a>
<p><?php echo $page->post_content ?>
</li>
<?php } ?>