//first image from post
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];
echo get_first_image()
This code above grabs the first image from a post, no big deal. It is a very common code found. I am having trouble grabbing the first image from a child page. I want users to go to the parent page and see the child pages names with the first image from that page. Any help or know how to do this?
<?php $child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY menu_order,post_title", 'OBJECT'); ?>
<?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>
<h2 class="subpagetitle"><a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a></h2>
<!--And if you want to get the custom field values then you can get that by
$your_custom_field = get_post_meta($pageChild->ID, 'your_custom_field', $single = true);-->
<?php the_excerpt();?>
<?php endforeach; endif; ?>
^This is what I am using to get child pages and their excerpts but I also need it to grab the first image from those child pages.
Any idea?
Nick Parsons answers:
I'd suggest this:
function get_first_image($content) {
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
$first_img = $matches [1] [0];
return $first_img;
}
<?php $child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY menu_order,post_title", 'OBJECT'); ?>
<?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>
<h2 class="subpagetitle"><a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a></h2>
<?php echo get_first_image($pageChild->post_content); ?>
<!--And if you want to get the custom field values then you can get that by
$your_custom_field = get_post_meta($pageChild->ID, 'your_custom_field', $single = true);-->
<?php the_excerpt();?>
<?php endforeach; endif; ?>
Zach Reed comments:
Perfect, thanks!
Baki Goxhaj answers:
Try this:
<?php $child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY menu_order,post_title", 'OBJECT'); ?>
<?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>
<h2 class="subpagetitle"><a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a></h2>
<?php echo get_first_image() ?>
<?php the_excerpt();?>
<?php endforeach; endif; ?>
Zach Reed comments:
Your answer just grabbed the current page image, Nick (below)'s answer grabbed what I needed, sorry.