调用子页面缩略图图像

时间:2013-03-04 22:24:25

标签: php wordpress

我在Wordpress网站上使用以下代码。短代码查找当前页面的子页面并返回其标题和摘录。我正在尝试集成页面缩略图,但它返回父页面的缩略图。有任何想法吗?

  add_shortcode("children-excerpt", "sc_show_children_excerpt");

   function sc_show_children_excerpt($atts, $content = null){
if($atts['length'] > 0 ){
    //maybe set a minimum length here

   }else{
    $atts['length'] = 50;
}


if(isset($atts['id']) && is_numeric($atts['id'])){
    //id specified by shortcode attribute
    $parent_id = $atts['id'];
}else{
    //get the id of the current article that is calling the shortcode
    $parent_id = get_the_ID();
}


$output = "";

$i = 0;

if ( $children = get_children(array(
    'post_parent' => $parent_id,
    'orderby' => 'menu_order',
    'order' => 'ASC',
    'post_type' => 'page')))
{
    foreach( $children as $child ) {
        $title = $child->post_title;

        $child_excerpt = apply_filters('the_excerpt', $child->post_excerpt);

        //split excerpt into array for processing
        $words = explode(' ', $child_excerpt);

        //chop off the excerpt based on the atts->lenth
        $words = array_slice($words, 0, $atts['length']);

        //merge the array of words for the excerpt back into sentances
        $child_excerpt = implode(' ', $words);

        $link = get_permalink($child->ID);

        $test = wp_get_attachment_image_src( $post->ID, 'thumbnail' );          
        $image_id = get_post_thumbnail_id($post->ID);  
        $image_url = wp_get_attachment_image_src($image_id,'medium');  
        $image_url = $image_url[0]; 
        $result = '<img src="'.$image_url.'" class="alignleft" />';

        $output .= "<div class='attorney'>";
        $output .= $result;
        $output .= "<a href='$link'>$title</a>";
        $output .= "<p>". $child_excerpt ."</p>";
        $output .= "</div><div class='clear'></div>";
    }
} 

return $output;

}

1 个答案:

答案 0 :(得分:0)

这应该让您的孩子缩略图,使用此

   $image_url = get_the_post_thumbnail($child->ID,'medium'); 
   $result = $image_url;

而不是

   $image_url = wp_get_attachment_image_src($image_id,'medium');  
   $image_url = $image_url[0]; 
   $result = '<img src="'.$image_url.'" class="alignleft" />';