我在一个机密的网站上工作..如果用户上传了图片,它会显示缩略图,但我想添加一个默认的缩略图,如果图像没有上传..我是新手,我无法编码wp template..Thank You
View File
<div class="thumb">
<?php
colabs_image('key=image&width=150&height=154');
?>
</div>
function colabs_image($args) {
/* ------------------------------------------------------------------------- */
/* SET VARIABLES $key = get_option('colabs_custom_field_image');*/
/* ------------------------------------------------------------------------- */
global $post;
global $colabs_options;
//Defaults
if (get_option('colabs_custom_field_image')!=''){
$key = '<img border="0" src="http://mmpropertydevelopers.com/wp-content/uploads/2013/12/thumbnail-default.jpg" alt="Image" width="100" height="100" />';
}else{
$key = 'image';
}
$width = null;
$height = null;
$class = '';
$quality = 90;
$id = null;
$link = 'src';
$repeat = 1;
$offset = 0;
$before = '';
$after = '';
$single = false;
$force = true;
$return = false;
$is_auto_image = false;
$src = '';
$meta = '';
$alignment = '';
$size = '';
$play = false;
$alt = '';
$img_link = '';
$attachment_id = array();
$attachment_src = array();
if ( !is_array($args) )
parse_str( $args, $args );
extract($args);
// Set Play Icon
$playicon = '';
// Set post ID
if ( empty($id) ) {
$id = $post->ID;
}
$thumb_id = get_post_meta($id,'_thumbnail_id',true);
// Set alignment
if ( '' == $alignment)
$alignment = get_post_meta($id, '_image_alignment', true);
// Get standard sizes
if ( !$width && !$height ) {
$width = '100';
$height = '100';
}
/* ------------------------------------------------------------------------- */
/* FIND IMAGE TO USE */
/* ------------------------------------------------------------------------- */
// When a custom image is sent through
if ( $src != '' ) {
$custom_field = $src;
$link = 'img';
// WP 2.9 Post Thumbnail support
} elseif ( 'true' == get_option( 'colabs_post_image_support') AND !empty($thumb_id) ) {
if ( get_option( 'colabs_pis_resize') == "true") {
// Dynamically resize the post thumbnail
$vt_crop = get_option( 'colabs_pis_hard_crop' );
if ($vt_crop == "true") $vt_crop = true; else $vt_crop = false;
$vt_image = vt_resize( $thumb_id, '', $width, $height, $vt_crop );
// Set fields for output
$custom_field = $vt_image['url'];
$width = $vt_image['width'];
$height = $vt_image['height'];
} else {
// Use predefined size string
if ( $size )
$thumb_size = $size;
else
$thumb_size = array($width,$height);
$img_link = get_the_post_thumbnail($id,$thumb_size,array( 'class' => 'colabs-image ' . $class));
}
// Grab the image from custom field
} else {
$custom_field = get_post_meta($id, $key, true);
}
// Automatic Image Thumbs - get first image from post attachment
if ( empty($custom_field) && 'true' == get_option( 'colabs_auto_img') && empty($img_link) && !(is_singular() AND in_the_loop() AND $link == "src") ) {
if( $offset >= 1 )
$repeat = $repeat + $offset;
$attachments = get_children( array( 'post_parent' => $id,
'numberposts' => $repeat,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'DESC',
'orderby' => 'menu_order date')
);
// Search for and get the post attachment
if ( !empty($attachments) ) {
$counter = -1;
$size = 'large';
foreach ( $attachments as $att_id => $attachment ) {
$counter++;
if ( $counter < $offset )
continue;
if ( 'true' == get_option('colabs_post_image_support') AND get_option( 'colabs_pis_resize') == "true") {
// Dynamically resize the post thumbnail
$vt_crop = get_option( 'colabs_pis_hard_crop' );
if ($vt_crop == "true") $vt_crop = true; else $vt_crop = false;
$vt_image = vt_resize( $att_id, '', $width, $height, $vt_crop );
// Set fields for output
$custom_field = $vt_image['url'];
$width = $vt_image['width'];
$height = $vt_image['height'];
} else {
$src = wp_get_attachment_image_src($att_id, $size, true);
$custom_field = $src[0];
$attachment_id[] = $att_id;
$src_arr[] = $custom_field;
}
$thumb_id = $att_id;
$is_auto_image = true;
}
// Get the first img tag from content
} else {
$first_img = '';
$post = get_post($id);
ob_start();
ob_end_clean();
$output = preg_match_all( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
if ( !empty($matches[1][0]) ) {
// Save Image URL
$custom_field = $matches[1][0];
// Search for ALT tag
$output = preg_match_all( '/<img.+alt=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
if ( !empty($matches[1][0]) ) {
$alt = $matches[1][0];
}
}
}
}
// Check if there is YouTube embed
if ( empty($custom_field) && empty($img_link) ) {
$embed = get_post_meta($id, "colabs_embed", true);
if ( $embed ) {
$custom_field = colabs_get_video_image($embed);
// Set Play Icon
if($play == true){ $playicon = '<span class="playicon">Play</span>'; }
}
}
// Return if there is no attachment or custom field set
if ( empty($custom_field) && empty($img_link) ) {
// Check if default placeholder image is uploaded
$placeholder = get_option( 'framework_colabs_default_image' );
if ( $placeholder && !(is_singular() AND in_the_loop()) ) {
$custom_field = $placeholder;
// Resize the placeholder if
if ( 'true' == get_option('colabs_post_image_support') AND get_option( 'colabs_pis_resize') == "true") {
// Dynamically resize the post thumbnail
$vt_crop = get_option( 'colabs_pis_hard_crop' );
if ($vt_crop == "true") $vt_crop = true; else $vt_crop = false;
$vt_image = vt_resize( '', $placeholder, $width, $height, $vt_crop );
// Set fields for output
$custom_field = $vt_image['url'];
$width = $vt_image['width'];
$height = $vt_image['height'];
}
} else {
return;
}
}
if(empty($src_arr) && empty($img_link)){ $src_arr[] = $custom_field; }
/* ------------------------------------------------------------------------- */
/* BEGIN OUTPUT */
/* ------------------------------------------------------------------------- */
$output = '';
// Set output height and width
$set_width = ' width="' . $width .'" ';
$set_height = ' height="' . $height .'" ';
if($height == null OR '' == $height) $set_height = '';
// Set standard class
if ( $class ) $class = 'colabs-image ' . $class; else $class = 'colabs-image';
// Do check to verify if images are smaller then specified.
if($force == true){ $set_width = ''; $set_height = ''; }
// WP Post Thumbnail
if(!empty($img_link) ){
if( 'img' == $link ) { // Output the image without anchors
$output .= $before;
$output .= $img_link;
$output .= $after;
} elseif( 'url' == $link ) { // Output the large image
$src = wp_get_attachment_image_src($thumb_id, 'large', true);
$custom_field = $src[0];
$output .= $custom_field;
} else { // Default - output with link
if ( ( is_single() OR is_page() ) AND $single == false ) {
$rel = 'rel="lightbox"';
$href = false;
} else {
$href = get_permalink($id);
$rel = '';
}
$title = 'title="' . get_the_title($id) .'"';
$output .= $before;
if($href == false){
$output .= $img_link;
} else {
$output .= '<a '.$title.' href="' . $href .'" '.$rel.'>' . $img_link . '</a>';
}
$output .= $after;
}
}
// Use thumb.php to resize. Skip if image has been natively resized with vt_resize.
elseif ( 'true' == get_option( 'colabs_resize') && empty($vt_image['url']) ) {
foreach($src_arr as $key => $custom_field){
// Clean the image URL
$href = $custom_field;
$custom_field = cleanSource( $custom_field );
// Check if WPMU and set correct path AND that image isn't external
if ( function_exists( 'get_current_site') && strpos($custom_field,"http://") !== 0 ) {
get_current_site();
//global $blog_id; Breaks with WP3 MS
if ( !$blog_id ) {
global $current_blog;
$blog_id = $current_blog->blog_id;
}
if ( isset($blog_id) && $blog_id > 0 ) {
$imageParts = explode( 'files/', $custom_field );
if ( isset($imageParts[1]) )
$custom_field = '/blogs.dir/' . $blog_id . '/files/' . $imageParts[1];
}
}
//Set the ID to the Attachment's ID if it is an attachment
if($is_auto_image == true){
$quick_id = $attachment_id[$key];
} else {
$quick_id = $id;
}
//Set custom meta
if ($meta) {
$alt = $meta;
$title = 'title="'. $meta .'"';
} else {
if ('' == $alt AND get_post_meta($thumb_id, '_wp_attachment_image_alt', true) )
$alt = get_post_meta($thumb_id, '_wp_attachment_image_alt', true);
else
$alt = get_the_title($quick_id);
$title = 'title="'. get_the_title($quick_id) .'"';
}
// Set alignment parameter
if ($alignment <> '')
$alignment = '&a='.$alignment;
$img_link = '<img src="'. get_template_directory_uri(). '/functions/timthumb.php?src='. $custom_field .'&w='. $width .'&h='. $height .'&zc=1&q='. $quality . $alignment . '" alt="'.$alt.'" class="'. stripslashes($class) .'" '. $set_width . $set_height . ' />';
if( 'img' == $link ) { // Just output the image
$output .= $before;
$output .= $img_link;
$output .= $playicon;
$output .= $after;
} elseif( 'url' == $link ) { // Output the image without anchors
if($is_auto_image == true){
$src = wp_get_attachment_image_src($thumb_id, 'large', true);
$custom_field = $src[0];
}
$output .= $custom_field;
} else { // Default - output with link
if ( ( is_single() OR is_page() ) AND $single == false ) {
$rel = 'rel="lightbox"';
} else {
$href = get_permalink($id);
$rel = '';
}
$output .= $before;
$output .= '<a '.$title.' href="' . $href .'" '.$rel.'>' . $img_link . $playicon . '</a>';
$output .= $after;
}
}
// No dynamic resizing
} else {
foreach($src_arr as $key => $custom_field){
//Set the ID to the Attachment's ID if it is an attachment
if($is_auto_image == true AND isset($attachment_id[$key])){
$quick_id = $attachment_id[$key];
} else {
$quick_id = $id;
}
//Set custom meta
if ($meta) {
$alt = $meta;
$title = 'title="'. $meta .'"';
} else {
if ('' == $alt) $alt = get_post_meta($thumb_id, '_wp_attachment_image_alt', true);
$title = 'title="'. get_the_title($quick_id) .'"';
}
$img_link = '<img src="'. $custom_field .'" alt="'. $alt .'" '. $set_width . $set_height . ' class="'. stripslashes($class) .'" />';
if ( 'img' == $link ) { // Just output the image
$output .= $before;
$output .= $img_link;
$output .= $after;
} elseif( 'url' == $link ) { // Output the URL to original image
if ( $vt_image['url'] || $is_auto_image ) {
$src = wp_get_attachment_image_src($thumb_id, 'full', true);
$custom_field = $src[0];
}
$output .= $custom_field;
} else { // Default - output with link
if ( ( is_single() OR is_page() ) AND $single == false ) {
// Link to the large image if single post
if ( $vt_image['url'] || $is_auto_image ) {
$src = wp_get_attachment_image_src($thumb_id, 'full', true);
$custom_field = $src[0];
}
$href = $custom_field;
$rel = 'rel="lightbox"';
} else {
$href = get_permalink($id);
$rel = '';
}
$output .= $before;
$output .= '<a href="' . $href .'" '. $rel . $title .'>' . $img_link . '</a>';
$output .= $after;
}
}
}
// Return or echo the output
if ( $return == TRUE )
return $output;
else
echo $output; // Done
}
/* Get thumbnail from Video Embed code */
答案 0 :(得分:0)
您可以在图片中添加javascript onError
。
$key = '<img border="0" src="mypic.jpg" alt="Image" width="100" height="100"
onError="this.onerror=null;this.src='.chr(39).'alternative.jpg'.chr(39).'"/>';