为主题添加默认图像的功能也可以在Trac上找到以下代码作为票证1)
请将您自己添加为@cc以支持它。谢谢
Ticket Link
使用函数
wp_default_img()
:
它使用属性的输入数组,并提供两个过滤器(wp\\u default\\u img\\u attr&wp\\u default\\u img)。因此,设置默认图像就像使用过滤器一样简单(如果主题开发人员对默认参数的功能不满意),最后只需添加…
// functions.php during init:
add_image_size( \'default_img\', 80, 80, true );
// Inside some template
$placeholder = get_site_url( null, \'your_path\' ).\'/some_img.jpg\';
echo wp_default_img( array( \'url\' => $placeholder, \'size\' => \'default_img\' ) );
当使用add\\u image\\u size();;注册大小时,如果第4个参数设置为true,该函数还关心裁剪图像;。
function wp_default_img( $attr )
{
// Sizes registered via add_image_size();
global $_wp_additional_image_sizes;
$defaults = array(
\'size\' => \'medium\'
,\'classes\' => false
,\'alt\' => false
,\'title\' => false
,\'align\' => \'none\'
,\'echo\' => true
);
$attr = wp_parse_args( $attr, $defaults );
if ( \'thumb\' === $attr[\'size\'] )
$attr[\'size\'] = \'thumbnail\';
// Size in built in sizes - call size setting from DB
# behavoir in here, dependent on outcome of @link http://core.trac.wordpress.org/ticket/18947
if ( ! in_array( $attr[\'size\'], array_keys( $_wp_additional_image_sizes ) ) )
{
$sizes = get_intermediate_image_sizes();
// Get option - gladly autoloaded/can use wp_cache_get();
$size_data[\'width\'] = intval( get_option( "{$attr[\'size\']}_size_w") );
$size_data[\'height\']= intval( get_option( "{$attr[\'size\']}_size_h") );
// Not sure how this will behave if cropped is false (autoloaded option not added)
$size_data[\'crop\'] = get_option( "{$attr[\'size\']}_crop" ) ? get_option( "{$attr[\'size\']}_crop" ) : false;
}
// Size array from global registered additional/custom sizes array
else
{
$size_data = $_wp_additional_image_sizes[ $attr[\'size\'] ];
}
// Retrieve image width & height
$img_info = @getimagesize( $attr[\'url\'] );
// Calculate final dimensions - if "crop" was set to true during add_image_size(), the img will get ... cropped
$end_sizes = image_resize_dimensions( $img_info[0], $img_info[1], $size_data[\'width\'], $size_data[\'height\'], $size_data[\'crop\'] );
// defaults to px units - can\'t get changed, as applying units is not possible
$hwstring = \' \'.trim( image_hwstring( $end_sizes[4], $end_sizes[5] ) );
// Attributes:
// Not made required as users tend to do funky things (...and lock screen readers out)
$attr[\'alt\'] = $attr[\'alt\'] ? \' alt="\'.esc_attr( $attr[\'alt\'] ).\'"\' : \'\';
if ( ! $attr[\'title\'] )
{
$mime = explode( "/", $img_info[\'mime\'] );
$attr[\'title\'] = sprintf( __(\'default image of type: %1$s\'), ucfirst( $mime[1] ) );
}
$attr[\'title\'] = $attr[\'title\'] ? \' title="\'.esc_attr( $attr[\'title\'] ).\'"\' : \'\';
$attr[\'classes\'] = "wp-img-default ".esc_attr( $attr[\'classes\'] ? $attr[\'classes\'] : \'\' );
$attr[\'align\'] = $attr[\'align\'] ? "align".esc_attr( $attr[\'align\'] ) : \'\';
$attr[\'size\'] = "size-".esc_attr( $attr[\'size\'] );
// Allow filtering of the default attributes
$attributes = apply_filters( \'wp_default_img_attr\', $attr );
// Build class attribute, considering that maybe some attribute was unset via the filter
$classes = \' class="\';
$classes .= \'wp-img-default\'.esc_attr( $attr[\'classes\'] ? \' \'.$attr[\'classes\'] : \'\' );
$classes .= $attr[\'align\'] ? \' \'.esc_attr( $attr[\'align\'] ) : \'\';
$classes .= $attr[\'size\'] ? \' \'.esc_attr( $attr[\'size\'] ).\'" \' : \'" \';
$url = trim( $attr[\'url\'] );
$image = "<img src=\'{$url}\'{$hwstring}{$classes}{$attr[\'alt\']}{$attr[\'title\']} />";
// Allow filtering of output
$image = apply_filters( \'wp_default_img\', $image );
if ( ! $attr[\'echo\'] )
return $image;
return print $image;
}