新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
//去除默认的相册短
专业领域包括成都做网站、网站设计、成都做商城网站、微信营销、系统平台开发, 与其他网站设计及系统开发公司不同,创新互联的整合解决方案结合了帮做网络品牌建设经验和互联网整合营销的理念,并将策略和执行紧密结合,为客户提供全网互联网整合方案。
remove_shortcode(‘gallery’, ’gallery_shortcode’);
//添加相册的短代码
function itluren_gallery_shortcode(){
global $post ;
if (preg_match_all(‘/\[ga llery([\r\n]*?)ids= “([\s\S]*?)” \]/i’, $post -post_content, $matches )){
$pictures_ids = $matches [2][0];
$pictures_array = explode (‘,’, $pictures_ids );
$all_images_count_num = count ( $pictures_array );
$post_permalink_str =get_permalink( $post -ID);
$options = get_option(‘itluren’);
$post_title_str = $post -post_title;
$baidu_share_sina = $options ['mts_baidu_share_sina'];
$baidu_share_qq = $options ['mts_baidu_share_qq'];
if ( $post_permalink_str != ” ){
$post_permalink_str_u =urlencode( $post_permalink_str );
}
if ( $post -post_title=! ” ){
$post_title_str_u =urlencode( $post -post_title);
}
for ( $i =0; $i count ( $pictures_array ); $i ++){
$picture_number = $i +1;
$image_information =wp_get_attachment_image_src( $pictures_array [ $i ]);
$image_altstr =get_post_meta((int) $pictures_array [ $i ],’_wp_attachment_image_alt’,true);
$image_data =itluren_get_attachment((int) $pictures_array [ $i ]);
$image_caption = $image_data ['caption'];
$image_des = $image_data ['description'];
$image_title = $image_data ['title'];
if ( $image_altstr == ” ){
if ( $image_des != ” ){
$image_altstr = $image_des ;
} else {
$image_altstr =’《’. $post_title_str .’》第’. $picture_number .’张’;
}
}
if ( $image_altstr != ” ){
$image_alt_str_u =urlencode( $image_altstr );
}
$image_url_str_u =urlencode( $image_information [0]);
if ( $image_caption != ” ){
$image_desc = $image_caption ;
} else if ( $image_des != ” ){
$image_desc = $image_des ;
} else {
$image_desc =’暂无说明或者描述’;
}
if ( $image_title != ” ){
$image_a_title = $image_title ;
} else {
$image_a_title =’《’. $post_title_str .’》第’. $picture_number .’张’;
}
用一下几个插件就可以了
1.Coppermine相册
简介:Coppermine Gallery本身是个简便易用的php相册,而后推出的psnGallery插件使得WordPress用户可以很方便地在blog里面插入图片。
好处:psnGallery的功能非常强大,是图片blog用户的首选。
缺陷:Coppermine Gallery缺省安装的界面非常复杂。
2.Menalto Gallery相册
简介:这个相册可能是最受欢迎的网站相册软件了,非常容易安装,而且支持自定义的html镶嵌方式(通过修改html_wrap/wrapper.header文件),有各种上载方式,适合图片文件很多的用户。
好处:功能多,可以让整个网站风格一致。
缺陷:没有专门给WordPress用户的插件,要在blog里引用图片还需要剪贴代码。
3.Pictorialis插件
简介:这个插件是专门为了把WordPress变成图片blog网站而开发的,可以从预设的目录里读取图片文件,直接生成blog正文。
好处:方便需要每天上载图片的用户。
缺陷:不太适合图片非常多,只想在正文中引用少数图片的用户,上载功能比较有限。
图片信息实际仍存储在posts表中,post_type为attachment
后台相册编辑界面里没有专门的栏目用于填写外链地址,不过可以通过其他字段实现啊
如下图,在alt text字段里填上该图片的外链
接着,需要参考wp-includes/media.php文件里的gallery_shortcode函数来重构gallery的输出
在functions.php中添加代码如下:
add_filter('post_gallery', 'my_post_gallery', 10, 3);
function my_post_gallery( $html = '', $attr, $instance ) {
$post = get_post();
$html5 = current_theme_supports( 'html5', 'gallery' );
$atts = shortcode_atts( array(
'order' = 'ASC',
'orderby' = 'menu_order ID',
'id' = $post ? $post-ID : 0,
'itemtag' = $html5 ? 'figure' : 'dl',
'icontag' = $html5 ? 'div' : 'dt',
'captiontag' = $html5 ? 'figcaption' : 'dd',
'columns' = 3,
'size' = 'thumbnail',
'include' = '',
'exclude' = '',
'link' = 'none'
), $attr, 'gallery' );
$id = intval( $atts['id'] );
if ( ! empty( $atts['include'] ) ) {
$_attachments = get_posts( array( 'include' = $atts['include'], 'post_status' = 'inherit', 'post_type' = 'attachment', 'post_mime_type' = 'image', 'order' = $atts['order'], 'orderby' = $atts['orderby'] ) );
$attachments = array();
foreach ( $_attachments as $key = $val ) {
$attachments[$val-ID] = $_attachments[$key];
}
} elseif ( ! empty( $atts['exclude'] ) ) {
$attachments = get_children( array( 'post_parent' = $id, 'exclude' = $atts['exclude'], 'post_status' = 'inherit', 'post_type' = 'attachment', 'post_mime_type' = 'image', 'order' = $atts['order'], 'orderby' = $atts['orderby'] ) );
} else {
$attachments = get_children( array( 'post_parent' = $id, 'post_status' = 'inherit', 'post_type' = 'attachment', 'post_mime_type' = 'image', 'order' = $atts['order'], 'orderby' = $atts['orderby'] ) );
}
if ( empty( $attachments ) ) {
return '';
}
if ( is_feed() ) {
$output = "\n";
foreach ( $attachments as $att_id = $attachment ) {
$output .= wp_get_attachment_link( $att_id, $atts['size'], true ) . "\n";
}
return $output;
}
$itemtag = tag_escape( $atts['itemtag'] );
$captiontag = tag_escape( $atts['captiontag'] );
$icontag = tag_escape( $atts['icontag'] );
$valid_tags = wp_kses_allowed_html( 'post' );
if ( ! isset( $valid_tags[ $itemtag ] ) ) {
$itemtag = 'dl';
}
if ( ! isset( $valid_tags[ $captiontag ] ) ) {
$captiontag = 'dd';
}
if ( ! isset( $valid_tags[ $icontag ] ) ) {
$icontag = 'dt';
}
$columns = intval( $atts['columns'] );
$itemwidth = $columns 0 ? floor(100/$columns) : 100;
$float = is_rtl() ? 'right' : 'left';
$selector = "gallery-{$instance}";
$gallery_style = '';
/**
* Filters whether to print default gallery styles.
*
* @since 3.1.0
*
* @param bool $print Whether to print default gallery styles.
* Defaults to false if the theme supports HTML5 galleries.
* Otherwise, defaults to true.
*/
if ( apply_filters( 'use_default_gallery_style', ! $html5 ) ) {
$gallery_style = "
style type='text/css'
#{$selector} {
margin: auto;
}
#{$selector} .gallery-item {
float: {$float};
margin-top: 10px;
text-align: center;
width: {$itemwidth}%;
}
#{$selector} img {
border: 2px solid #cfcfcf;
}
#{$selector} .gallery-caption {
margin-left: 0;
}
/* see gallery_shortcode() in wp-includes/media.php */
/style\n\t\t";
}
$size_class = sanitize_html_class( $atts['size'] );
$gallery_div = "div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'";
/**
* Filters the default gallery shortcode CSS styles.
*
* @since 2.5.0
*
* @param string $gallery_style Default CSS styles and opening HTML div container
* for the gallery shortcode output.
*/
$output = apply_filters( 'gallery_style', $gallery_style . $gallery_div );
$i = 0;
foreach ( $attachments as $id = $attachment ) {
$attr = ( trim( $attachment-post_excerpt ) ) ? array( 'aria-describedby' = "$selector-$id" ) : '';
if ( ! empty( $atts['link'] ) 'file' === $atts['link'] ) {
$image_output = wp_get_attachment_link( $id, $atts['size'], false, false, false, $attr );
} elseif ( ! empty( $atts['link'] ) 'none' === $atts['link'] ) {
$attr['alt'] = get_the_title($post-ID);
$image_output = wp_get_attachment_image( $id, $atts['size'], false, $attr );
$custom_link = trim( strip_tags( get_post_meta( $id, '_wp_attachment_image_alt', true ) ) );
$image_output = sprintf( 'a href="%s" title="%s"%sa',
$custom_link,
$attr['alt'],
$image_output
);
} else {
$image_output = wp_get_attachment_link( $id, $atts['size'], true, false, false, $attr );
}
$image_meta = wp_get_attachment_metadata( $id );
$orientation = '';
if ( isset( $image_meta['height'], $image_meta['width'] ) ) {
$orientation = ( $image_meta['height'] $image_meta['width'] ) ? 'portrait' : 'landscape';
}
$output .= "{$itemtag} class='gallery-item'";
$output .= "
{$icontag} class='gallery-icon {$orientation}'
$image_output
/{$icontag}";
if ( $captiontag trim($attachment-post_excerpt) ) {
$output .= "
{$captiontag} class='wp-caption-text gallery-caption' id='$selector-$id'
" . wptexturize($attachment-post_excerpt) . "
/{$captiontag}";
}
$output .= "/{$itemtag}";
if ( ! $html5 $columns 0 ++$i % $columns == 0 ) {
$output .= 'br style="clear: both" /';
}
}
if ( ! $html5 $columns 0 $i % $columns !== 0 ) {
$output .= "
br style='clear: both' /";
}
$output .= "
/div\n";
return $output;
}
最后的效果:
WordPress的相册,这个要看你的模板,如果你上传模板本省就有相册页面的话,就直接在文章里面上传图片就好了。
如果你是想自己在边栏添加图片的话可以在后台--外观--小工具--添加文本--然后可以在图片里面放入你的图片目录地址。
如果你是放在某些特定的位置的话,还有可能会把图片嵌入到代码之中,当然,这个需要你懂一定的代码,不然的话,整个网站就会出问提