虽然核心中没有内置此类功能,但Track Ticket 我两年前求婚了。您可以简单地从建议的wp_default_img()
作用我将非常感谢大家对这张票的支持,因为这对于每个主题开发人员来说都是非常需要的。提前谢谢。
MU Plugin
以下代码将被放入
~/wp-content/mu-plugins
文件夹(或任何您命名的文件夹)。默认情况下,它将在网络安装的每个站点中可用,或者在单个站点安装的任何地方都可用。使用方法如下:
default_img( array( /* attributes - see doc block */ ) );
只需将下面的代码以任何名称保存到新文件中,您就可以开始了。
<?php
defined( \'ABSPATH\' ) OR exit;
/**
* Plugin Name: Default Image template tag
* Plugin URI: http://unserkaiser.com
* Description: Adds a default image template tag
* Version: 0.1
* Author: Franz Josef Kaiser
* Author URI: http://unserkaiser.com
* License: GNU GPL 2 <https://gist.github.com/1365159>
*/
/**
* Default image
*
* Builds an default <img> for use in themes or plugins before any other images are added.
* Resizes & crops the image using the built-in (retireved via `get_intermediate_image_sizes();`)
* or custom image (added via `add_image_size();`) sizes.
*
* Retrieves calculated resize dimension @uses image_resize_dimensions();
* Builds the width and height string @uses image_hwstring();
*
* @param $attr
* @internal param $args (array)
* string $url URl to the given default image.
* string $size Optional. Default is \'medium\'.
* string (optional) $alt Image Description for the alt attribute.
* string (optional) $title Image Description for the title attribute.
* string (optional) $align Part of the class name for aligning the image.
* string (optional) $echo Wheter to return or echo the $image
* @return string HTML IMG element for given image attachment
*/
function default_img( $attr )
{
// Sizes registered via add_image_size();
global $_wp_additional_image_sizes;
$defaults = array(
\'size\' => \'medium\',
\'classes\' => false,
\'alt\' => \'\',
\'title\' => \'\',
\'align\' => \'none\',
\'echo\' => true,
);
$attr = wp_parse_args( $attr, $defaults );
$attr = array_map( \'esc_attr\', $attr );
if ( \'thumb\' === $attr[\'size\'] )
$attr[\'size\'] = \'thumbnail\';
// Size in built in sizes - call size setting from DB
# behavoir in here dependent on @link http://core.trac.wordpress.org/ticket/18947
# if in core, we change to:
# $sizes = get_intermediate_image_sizes();
# $size_data = $sizes[ $size ];
if ( ! in_array(
$attr[\'size\'],
array_keys( $_wp_additional_image_sizes )
) )
{
# @TODO delete?
$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)
if ( ! $size_data[\'crop\'] = get_option( "{$attr[\'size\']}_crop" ) )
$attr[\'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)
! empty( $attr[\'alt\'] ) AND $attr[\'alt\'] = " alt=\'{$attr[\'alt\']}\'";
if ( ! $attr[\'title\'] )
{
$mime = explode( "/", $img_info[\'mime\'] );
$attr[\'title\'] = sprintf( __( \'default image of type: %1$s\' ), ucfirst( $mime[1] ) );
}
$attr[\'title\'] = " title=\'{$attr[\'title\']}\'";
$attr[\'align\'] = " align{$attr[\'align\']}";
$attr[\'size\'] = " size-{$attr[\'size\']}";
// Allow filtering of the default attributes
$attr = apply_filters( \'wp_default_img_attr\', $attr );
// Build class attribute, considering that maybe some attribute was unset via the filter
$classes = "class=\'wp-img-default{$attr[\'align\']}{$attr[\'classes\']}{$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 );
$attr[\'echo\'] AND print $image;
return $image;
}