使用挂钩的_POST_缩略图回退

时间:2012-09-11 作者:daniel.tosaba

我使用的这个大主题几乎完全依赖于小部件和滑块。现在我试图避免寻找post_thumbnail 在所有包含的文件中输出代码,以实现回退,然后回退应该在post中获取第一个图像,或者最后显示默认图像。

有没有办法通过functions 文件

感谢您的帮助。

Solution based on post_thumbnail_html filter hook doesn\'t display featured image if it is not explicitly set:

add_filter( \'post_thumbnail_html\', \'my_post_thumbnail_fallback\', 20, 5 );
function my_post_thumbnail_fallback( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
    if ( empty( $html ) ) {
        $image = get_children( "post_parent={$post_id}&post_type=attachment&post_mime_type=image&numberposts=1" );
        if($image){
                foreach ($image as $attachment_id => $attachment) {
                        $src = wp_get_attachment_image_src($attachment_id);
                 return printf(
                                 \'<img src="%s" height="%s" width="%s" />\'
                                ,$src[0]
                                ,get_option( \'thumbnail_size_w\' )
                                ,get_option( \'thumbnail_size_h\' )
                                );
            }
         } 
         else {
               return printf(
                                 \'<img src="%s" height="%s" width="%s" />\'
                                ,get_template_directory_uri().\'/images/featured/featured.jpg\'
                                ,get_option( \'thumbnail_size_w\' )
                                ,get_option( \'thumbnail_size_h\' )
                                );
         }  
    } 
        return $html;
}

Another solution that I found in this article and it\'s based on different action hooks does thing as intended which displays(sets) first attachment image in post as featured or in last case scenario displays(sets) default image as post\'s featured image:

function autoset_featured() {
          global $post;
          $already_has_thumb = has_post_thumbnail($post->ID);
              if (!$already_has_thumb)  {
              $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
                          if ($attached_image) {
                                foreach ($attached_image as $attachment_id => $attachment) {
                                set_post_thumbnail($post->ID, $attachment_id);
                                }
                           } else {
                                set_post_thumbnail($post->ID, \'414\');
                           }
                        }
      }  //end function
add_action(\'the_post\', \'autoset_featured\');
add_action(\'save_post\', \'autoset_featured\');
add_action(\'draft_to_publish\', \'autoset_featured\');
add_action(\'new_to_publish\', \'autoset_featured\');
add_action(\'pending_to_publish\', \'autoset_featured\');
add_action(\'future_to_publish\', \'autoset_featured\');
现在。。我喜欢这样post_thumbnail_html filter hook 解决方案,我对它不起作用很感兴趣。

感谢您的帮助。

3 个回复
最合适的回答,由SO网友:Bainternet 整理而成

您可以使用post_thumbnail_html 过滤器挂钩,将5个变量传递给挂钩函数:

  • $html - 帖子缩略图的输出html$post_id - 帖子ID.
  • $post_thumbnail_id - 图像的附件id$size - 请求的大小或默认大小$attr - 查询属性的字符串或数组

    add_filter( \'post_thumbnail_html\', \'my_post_thumbnail_fallback\', 20, 5 );
    function my_post_thumbnail_fallback( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
        if ( empty( $html ) ) {
            // return you fallback image either from post of default as html img tag.
        }
        return $html;
    }
    

SO网友:kaiser

作为对@Bainternet答案的补充:

从选项表中检索设置的大小。这意味着,返回的图像将与所有用户设置对齐。仅从调用默认拇指template 目录以绕过存在子主题且没有缩略图的情况。

/**
 * Default post thumbnail image.
 * 
 * @param  string $html The Output HTML of the post thumbnail
 * @param  int $post_id The post ID
 * @param  int $post_thumbnail_id The attachment id of the image
 * @param  string $size The size requested or default
 * @param  mixed string/array $attr Query string or array of attributes
 * @return string $html the Output HTML of the post thumbnail
 */
function wpse64763_post_thumbnail_fb( $html, $post_id, $post_thumbnail_id, $size, $attr )
{
    if ( empty( $html ) )
    {
        return sprintf(
            \'<img src="%s" height="%s" width="%s" />\',
            get_template_directory_uri().\'/path/to/default-thumb.png\',
            get_option( \'thumbnail_size_w\' ),
            get_option( \'thumbnail_size_h\' )
        );
    }

    return $html;
}
add_filter( \'post_thumbnail_html\', \'wpse64763_post_thumbnail_fb\', 20, 5 );

SO网友:WebCaos

好了,刚刚测试过,像这样的工作对我来说很好;)

    add_filter( \'post_thumbnail_html\', \'wc_post_thumbnail_fallback\', 20, 5 );
function wc_post_thumbnail_fallback( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
   if ($html) {
        return $html;
    }else {
        $args = array(
            \'numberposts\' => 1,
            \'order\' => \'ASC\',
            \'post_mime_type\' => \'image\',
            \'post_parent\' => $post_id,
            \'post_status\' => null,
            \'post_type\' => \'attachment\',
        );

        $images = get_children($args);

        if ($images) {
            foreach ($images as $image) {
                return wp_get_attachment_image($image->ID, $size);
            }
        }else{
            printf(\'<img src="%s" height="%s" width="%s" />\'
                ,get_template_directory_uri().\'/images/featured/featured.jpg\'
                ,get_option( \'thumbnail_size_w\' )
                ,get_option( \'thumbnail_size_h\' ));
        }

    }
}

结束

相关推荐

Functions.php:从博客中排除类别

所以很明显,如何从模板中排除某些类别,但我不想修改4个模板,使它们忽略某个类别。有没有一种方法可以将某个类别从阅读设置的“博客”集中排除?我正在将博客分配到名为“博客”的页面。。。但显然,档案和搜索也需要对这一超出类别的内容视而不见。我宁愿在里面做functions.php