【问题标题】:Calling child page thumbnail image调用子页面缩略图
【发布时间】:2013-03-05 08:27:18
【问题描述】:

我在我的 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;

}

【问题讨论】:

  • 你能解释一下你想要更好地实现什么吗?我想我收集到您正在尝试返回当前帖子的所有孩子的缩略图?这是正确的吗?
  • 是的,没错,乔希!

标签: php wordpress


【解决方案1】:

这应该得到你孩子的缩略图,使用这个

   $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" />';

【讨论】:

  • 大卫,那没用。它什么也没返回。原始代码返回父页面的特色图片,而不是子页面上的特色图片。
  • 它对我有用,我在本地测试过,你确定你的父子设置正确吗?
  • 对不起,大卫 - 你是对的!我不小心保留了 get_post_thumbnaiL_id,而不是将其换成 get_the_post_thumbnail。非常感谢!
  • 大卫,我注意到你在费城——我也是!你的交易是什么?你做Wordpress开发吗?我一直在寻找可以合作的开发者!
  • 是的,我做 wordpress dev、rails、php、javascript、html5 等,让我知道和你自己?
猜你喜欢
  • 1970-01-01
  • 2011-08-09
  • 2012-03-29
  • 2020-05-05
  • 1970-01-01
  • 2011-11-28
  • 2019-03-22
  • 1970-01-01
  • 2012-08-28
相关资源
最近更新 更多