原生相册增加自定义样式
为所有图片增加属性 1 2 3 4 5 6 7 8 add_filter(‘the_content…
为所有图片增加属性
| 
 1 
2 
3 
4 
5 
6 
7 
8 
 | 
 
add_filter(‘the_content’, ‘my_addlightboxrel’);
 
function my_addlightboxrel($content) {
 
       global $post;
 
       $pattern =“/<a(.*?)href=(‘|”)(.*?).(bmp|gif|jpeg|jpg|png)(‘|”)(.*?)>/i”;
 
       $replacement = ‘<a$1href=$2$3.$4$5 rel=”lightbox” title=”‘.$post->post_title.‘”$6>’;
 
       $content = preg_replace($pattern, $replacement, $content);
 
       return $content;
 
} 
 | 
图片附件增加class类名
| 
 1 
2 
3 
4 
 | 
 
function example_add_img_class( $class ) {
 
    return $class . ‘ img-fluid’;
 
} 
add_filter( ‘get_image_tag_class’, ‘example_add_img_class’ );
 
 | 
图片自定义
为文章里的图片增加class类名
| 
 1 
2 
3 
4 
5 
 | 
 
add_filter(‘get_image_tag_class’,‘wpse302130_add_image_class’);
 
function wpse302130_add_image_class ($class){
 
    $class .= ‘ img-fluid’;
 
    return $class;
 
    }
 
 | 
该函数不适用于已经存在的帖子。
js方法
| 
 1 
2 
3 
4 
5 
 | 
 <script> 
    $(function() {
 
        $(“body img”).addClass(“attachment-thumbnail size-thumbnail”);
 
    });
 
</script> 
 | 
如何将自定义类添加到附件链接
| 
 1 
2 
3 
4 
5 
6 
 | 
 
function add_class_attachment_link($html){
 
    $postid = get_the_ID();
 
    $html = str_replace(‘<a’,‘<a class=”isitwp”‘,$html);
 
    return $html;
 
} 
add_filter(‘wp_get_attachment_link’,‘add_class_attachment_link’,10,1);
 
 | 
自定义相册终极代码
示例一:
//自定义原生相册的样式
add_shortcode( ‘gallery’, ‘custom_gallery_shortcode’ );
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
 | 
 //自定义原生相册的样式 
add_filter( ‘use_default_gallery_style’, ‘__return_false’ );
 
function custom_gallery_shortcode( $attr = array(), $content = ” )
 
{ 
        $attr[‘itemtag’]        = “li”;
 
        $attr[‘icontag’]        = “”;
 
        $attr[‘captiontag’]     = “p”;
 
        // 运行原生库短代码回调:
 
        $html = gallery_shortcode( $attr );
 
        // 删除除a,img,li,p之外的所有标签
 
        $html = strip_tags( $html, ‘<a><img><li><p>’ );
 
        // 类名替代
 
        $from = array(
 
            “class=’gallery-item’”,
 
            “class=’gallery-icon landscape’”,
 
            ‘class=”attachment-thumbnail”‘,
 
            ‘a href=’,
 
        );
 
        $to = array(
 
            ‘class=”tnggallery1″‘,  //li的类名
 
            ‘class=”tnggallery2″‘,
 
            ‘class=”tnggallery3″‘,
 
            ‘a class=”swipebox” href=’, //a的类名
 
        );
 
        $html = str_replace( $from, $to, $html );
 
        // 删除宽度/高度属性:
 
        $html = preg_replace( ‘/(width|height)=”d*”s/’, “”, $html );
 
        // 将输出包装在ul标签中:
 
        $html = sprintf( ‘<ul class=”gallery”>%s</ul>’, $html );
 
        return $html;
 
} 
 | 
示例二:
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
 | 
 
add_shortcode(‘gallery’, ‘my_gallery_shortcode’);
 
function my_gallery_shortcode($attr) {
 
    $post = get_post();
 
static $instance = 0;
 
$instance++;
 
if ( ! empty( $attr[‘ids’] ) ) {
 
    // ‘ids’ is explicitly ordered, unless you specify otherwise.
 
    if ( empty( $attr[‘orderby’] ) )
 
        $attr[‘orderby’] = ‘post__in’;
 
    $attr[‘include’] = $attr[‘ids’];
 
} 
// Allow plugins/themes to override the default gallery template. 
$output = apply_filters(‘post_gallery’, ”, $attr);
 
if ( $output != ” )
 
    return $output;
 
// We’re trusting author input, so let’s at least make sure it looks like a valid orderby statement 
if ( isset( $attr[‘orderby’] ) ) {
 
    $attr[‘orderby’] = sanitize_sql_orderby( $attr[‘orderby’] );
 
    if ( !$attr[‘orderby’] )
 
        unset( $attr[‘orderby’] );
 
} 
extract(shortcode_atts(array(
 
    ‘order’      => ‘ASC’,
 
    ‘orderby’    => ‘menu_order ID’,
 
    ‘id’         => $post->ID,
 
    ‘itemtag’    => ‘dl’,
 
    ‘icontag’    => ‘dt’,
 
    ‘captiontag’ => ‘dd’,
 
    ‘columns’    => 3,
 
    ‘size’       => ‘thumbnail’,
 
    ‘include’    => ”,
 
    ‘exclude’    => ”
 
), $attr));
 
$id = intval($id);
 
if ( ‘RAND’ == $order )
 
    $orderby = ‘none’;
 
if ( !empty($include) ) {
 
    $_attachments = get_posts( array(‘include’ => $include, ‘post_status’ => ‘inherit’, ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image’, ‘order’ => $order, ‘orderby’ => $orderby) );
 
    $attachments = array();
 
    foreach ( $_attachments as $key => $val ) {
 
        $attachments[$val->ID] = $_attachments[$key];
 
    }
 
} elseif ( !empty($exclude) ) {
 
    $attachments = get_children( array(‘post_parent’ => $id, ‘exclude’ => $exclude, ‘post_status’ => ‘inherit’, ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image’, ‘order’ => $order, ‘orderby’ => $orderby) );
 
} else {
 
    $attachments = get_children( array(‘post_parent’ => $id, ‘post_status’ => ‘inherit’, ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image’, ‘order’ => $order, ‘orderby’ => $orderby) );
 
} 
if ( empty($attachments) )
 
    return ”;
 
if ( is_feed() ) {
 
    $output = “n”;
 
    foreach ( $attachments as $att_id => $attachment )
 
        $output .= wp_get_attachment_link($att_id, $size, true) . “n”;
 
    return $output;
 
} 
$itemtag = tag_escape($itemtag);
 
$captiontag = tag_escape($captiontag);
 
$icontag = tag_escape($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($columns);
 
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
 
$float = is_rtl() ? ‘right’ : ‘left’;
 
$selector = “gallery-{$instance}”;
 
$gallery_style = $gallery_div = ”;
 
if ( apply_filters( ‘use_default_gallery_style’, true ) )
 
    $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;
 
        }
 
    </style>
 
    <!– see gallery_shortcode() in wp-includes/media.php –>”;
 
$size_class = sanitize_html_class( $size );
 
$gallery_div = “<div id=’$selector’ class=’gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}’>”;
 
$output = apply_filters( ‘gallery_style’, $gallery_style . “ntt” . $gallery_div );
 
$i = 0;
 
foreach ( $attachments as $id => $attachment ) {
 
    $link = isset($attr[‘link’]) && ‘file’ == $attr[‘link’] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
 
    $output .= “<{$itemtag} class=’gallery-item’>”;
 
    $output .= “
 
        <{$icontag} class=’gallery-icon’> 
            $link 
        </{$icontag}>”;
 
    if ( $captiontag && trim($attachment->post_excerpt) ) {
 
        $output .= “
 
            <{$captiontag} class=’wp-caption-text gallery-caption’> 
            “ . wptexturize($attachment->post_excerpt) . “
 
            </{$captiontag}>”;
 
    }
 
    $output .= “</{$itemtag}>”;
 
    if ( $columns > 0 && ++$i % $columns == 0 )
 
        $output .= ‘<br style=”clear: both” />’;
 
} 
$output .= “
 
        <br style=’clear: both;’ /> 
    </div>n”;
 
return $output;
 
} 
 | 
示例三:
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
 | 
 /* JUST ADD THIS CODE TO YOUR THEMES functions.php */ 
/* Customize the last half, the output half to suit your projects needs */ 
add_filter(‘post_gallery’, ‘my_post_gallery’, 10, 2);
 
function my_post_gallery($output, $attr) {
 
    global $post;
 
    if (isset($attr[‘orderby’])) {
 
        $attr[‘orderby’] = sanitize_sql_orderby($attr[‘orderby’]);
 
        if (!$attr[‘orderby’])
 
            unset($attr[‘orderby’]);
 
    }
 
    extract(shortcode_atts(array(
 
        ‘order’ => ‘ASC’,
 
        ‘orderby’ => ‘menu_order ID’,
 
        ‘id’ => $post->ID,
 
        ‘itemtag’ => ‘dl’,
 
        ‘icontag’ => ‘dt’,
 
        ‘captiontag’ => ‘dd’,
 
        ‘columns’ => 3,
 
        ‘size’ => ‘thumbnail’,
 
        ‘include’ => ”,
 
        ‘exclude’ => ”
 
    ), $attr));
 
    $id = intval($id);
 
    if (‘RAND’ == $order) $orderby = ‘none’;
 
    if (!empty($include)) {
 
        $include = preg_replace(‘/[^0-9,]+/’, ”, $include);
 
        $_attachments = get_posts(array(‘include’ => $include, ‘post_status’ => ‘inherit’, ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image’, ‘order’ => $order, ‘orderby’ => $orderby));
 
        $attachments = array();
 
        foreach ($_attachments as $key => $val) {
 
            $attachments[$val->ID] = $_attachments[$key];
 
        }
 
    }
 
    if (empty($attachments)) return ”;
 
    // Here’s your actual output, you may customize it to your needs
 
    $output = “<div class=”slideshow-wrapper”>n”;
 
    $output .= “<div class=”preloader”></div>n”;
 
    $output .= “<ul data-orbit>n”;
 
    // Now you loop through each attachment
 
    foreach ($attachments as $id => $attachment) {
 
        // Fetch the thumbnail (or full image, it’s up to you)
 
//      $img = wp_get_attachment_image_src($id, ‘medium’); 
//      $img = wp_get_attachment_image_src($id, ‘my-custom-image-size’); 
        $img = wp_get_attachment_image_src($id, ‘full’);
 
        $output .= “<li>n”;
 
        $output .= “<img src=”{$img[0]}” width=”{$img[1]}” height=”{$img[2]}” alt=”” />n”;
 
        $output .= “</li>n”;
 
    }
 
    $output .= “</ul>n”;
 
    $output .= “</div>n”;
 
    return $output;
 
} 
 | 
WordPress 获取附件图片的标题、说明、代替文本、图像描述
get_post_meta 函数是用来取出自定义字段的值的函数,那么在附件图片中代替文本信息就是一个自定义的值。然后叶子以为图像描述也是自定义的值,结果在wp_postmeta没有发现。
获取图片标题
| 
 1 
 | 
 
get_the_title( $post_id)
 
 | 
获取图片的代替文本
| 
 1 
 | 
 
get_post_meta( $post_id, ‘_wp_attachment_image_alt’, true )
 
 | 
获取图片描述
| 
 1 
 | 
 
get_the_content( $post_id)
 
 | 
获取图片说明
| 
 1 
 | 
 
get_the_excerpt($post_id)
 
 | 
| 
 1 
2 
3 
4 
 | 
 
$alt = get_post_meta($attachment->ID, ‘_wp_attachment_image_alt’, true);
 
$image_title = $attachment->post_title;
 
$caption = $attachment->post_excerpt;
 
$description = $image->post_content;
 
 | 
https://stackoverflow.com/questions/15581674/get-image-caption-from-meta-box-query
WordPress 去除图片img标签的高度与宽度
WordPress 去除图片img标签的高度与宽度
| 
 1 
2 
3 
4 
5 
6 
 | 
 
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;
 
} 
 | 
wordpress图片img去除样式及长宽属性width、height、class
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
 | 
 //remove insert images attribute 
//add_filter( ‘the_content’, ‘fanly_remove_images_attribute’, 99 ); 
add_filter( ‘post_thumbnail_html’, ‘fanly_remove_images_attribute’, 10 );
 
add_filter( ‘image_send_to_editor’, ‘fanly_remove_images_attribute’, 10 );
 
function fanly_remove_images_attribute( $html ) {
 
	//$html = preg_replace( ‘/(width|height)=”d*”s/’, “”, $html );
 
	$html = preg_replace( ‘/width=”(d*)”s+height=”(d*)”s+class=”[^”]*”/’, “”, $html );
 
	$html = preg_replace( ‘/  /’, “”, $html );
 
	return $html;
 
} 
 | 
 
来源:
https://wordpress.stackexchange.com/questions/140681/how-to-get-clean-code-for-a-gallery
https://stackoverflow.com/questions/14585538/customise-the-wordpress-gallery-html-layout
https://gist.github.com/radiovisual/9070765
https://www.itbook5.com/2018/12/6504/
类别:WordPress开发、 
		本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

还没有任何评论,赶紧来占个楼吧!