wordpress开发:缩略图thumbnail.php

WordPress获取特色图像地址主要需要用到两个函数get_post_thumbnail_id和wp_get_attachment_image_src.下面是分别获取小、中、大、完整、指定图片规格的图片地址的方法。
[php]
//获取特色图像:小
<?php
$thumbnail_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), ‘thumbnail’);
echo $thumbnail_image_url[0];
?>
[/php]
 

获取特色图像:中
[php]
<?php
$medium_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), ‘medium’);
echo $medium_image_url[0];
?>
[/php]
 

获取特色图像:大
[php]
<?php
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), ‘large’);
echo $large_image_url[0];
?>
[/php]
 

获取特色图像:完整
[php]
<?php
$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), ‘full’);
echo $full_image_url[0];
?>
[/php]
 

获取特色图像:指定100×100
[php]
<?php
$array_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array(100,100));
echo $array_image_url[0];
?>
[/php]
 

调用指定尺寸的图片 去掉class
[php]
//Post Thumbnail Support调用指定尺寸+class
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( (‘news’) , array( ‘class’ => ‘img-responsive’ ) ); ?>
</a>
<?php endif; ?>
[/php]
 

补充上面:
[php]
the_post_thumbnail( (‘full’)
[/php]
 

调用完整的图像,如果是直接调用的话,
[php]
<?php if ( has_post_thumbnail() ) { the_post_thumbnail(‘indextab’); } ?>
[/php]
 

直接调用
[php]
<?php the_post_thumbnail(); ?>
[/php]
 

如果没有缩略图时调用其他图片
[php]
<?php if ( has_post_thumbnail() ) { ?>
<?php the_post_thumbnail(); ?>
<?php } else {?>
<img src="<?php bloginfo(‘template_url’); ?>/images/xx.jpg" />
<?php } ?>
[/php]
 

functions.php中的代码:
[php]
add_theme_support( ‘post-thumbnails’ );
set_post_thumbnail_size( 160, 161, true );
add_image_size( ‘indexso’, 400, 306, true ); //首页index展示
add_theme_support( ‘post-thumbnails’, array( ‘post’, ‘page’ ) );
[/php]
 

或许要需要移除WordPress文章图片的宽度和高度属性
[php]
add_filter( ‘post_thumbnail_html’, ‘remove_width_attribute’, 10 );
add_filter( ‘image_send_to_editor’, ‘remove_width_attribute’, 10 );
function remove_width_attribute( $html ) {
$html = preg_replace( ‘/(width|height)="\d*"\s/’, "", $html );
return $html;
}
[/php]
 

WordPress为文章内图片自动添加属性代码,加到主题functions.php文件中即可:为图片自动添加属性
[php]
add_filter(‘the_content’, ‘pirobox_gall_replace’);
function pirobox_gall_replace ($content)
{
global $post;
$pattern = "/<a(.*?)href=(‘|")([^>]*).(bmp|gif|jpeg|jpg|png)(‘|")(.*?)>(.*?)<\/a>/i";
$replacement = ‘<a$1href=$2$3.$4$5 class="cboxElement"$6>$7</a>’;
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
[/php]
 

这样每个文章内的图片都会自动附带class=”cboxElement”的属性。

使WordPress支持post thumbnail
[php]
if ( function_exists( ‘add_theme_support’ ) ) {
add_theme_support( ‘post-thumbnails’ );
}
[/php]
 

给文章里添加data-img-src属性待验证
[php]
function add_image_placeholders( $content ) {
// Don’t lazyload for feeds, previews, mobile
if( is_feed() || is_preview() || ( function_exists( ‘is_mobile’ ) && is_mobile() ) )
return $content;
// Don’t lazy-load if the content has already been run through previously
if ( false !== strpos( $content, ‘data-img-src’ ) )
return $content;
// In case you want to change the placeholder image
$placeholder_image = apply_filters( ‘lazyload_images_placeholder_image’, get_template_directory_uri() . ‘/images/loader.GIF’ );
// This is a pretty simple regex, but it works
$content = preg_replace( ‘#<img([^>]+?)src=[\’"]?([^\’"\s>]+)[\’"]?([^>]*)>#’, sprintf( ‘<img${1}src="%s" data-img-src="${2}"${3}><noscript><img${1}src="${2}"${3}></noscript>’, $placeholder_image ), $content );
return $content;
}
add_filter( ‘the_content’, ‘add_image_placeholders’, 99 );
[/php]
 

 

发表评论

邮箱地址不会被公开。 必填项已用*标注