Php 在Wordpress中删除图库图像的宽度和高度

Php 在Wordpress中删除图库图像的宽度和高度,php,wordpress,gallery,Php,Wordpress,Gallery,我正在修改WP安装 我正在使用一个修改过的图库短代码来显示图库,并使用一个过滤器来删除宽度和高度参数,这是保持页面响应所必需的 我的问题是,当我链接到附件页面(默认设置)时,库会正确显示。但是,当我将缩略图链接到文件时,过滤器不会删除宽度和高度参数 这是过滤器: /** remove gallery height and width*/ add_image_size( 'my_gallery_shortcode', 200, 120, true ); add_filter('wp_get_at

我正在修改WP安装

我正在使用一个修改过的图库短代码来显示图库,并使用一个过滤器来删除宽度和高度参数,这是保持页面响应所必需的

我的问题是,当我链接到附件页面(默认设置)时,库会正确显示。但是,当我将缩略图链接到文件时,过滤器不会删除宽度和高度参数

这是过滤器:

/** remove gallery height and width*/
add_image_size( 'my_gallery_shortcode', 200, 120, true );

add_filter('wp_get_attachment_link', 'remove_img_width_height', 10, 4);

function remove_img_width_height( $html, $post_id, $post_image_id, $post_thumbnail) {
if ($post_thumbnail=='my_gallery_shortcode'){
$html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );
}
return $html;
}

/** END remove gallery height and width*/
这是自定义库代码:

/** custom gallery code */

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'       => 'medium',
    '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 = '';

$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 . "\n\t\t" . $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}>";

}

$output .= "
    </div>\n";

return $output;
}
/**自定义库代码*/
添加_短代码(“图库”、“我的_图库_短代码”);
函数my_gallery_快捷码($attr){
$post=get_post();
静态$instance=0;
$instance++;
如果(!empty($attr['id'])){
//除非您另行指定,否则“ID”是显式排序的。
if(空($attr['orderby']))
$attr['orderby']='post_uuuin';
$attr['include']=$attr['id'];
}
//允许插件/主题覆盖默认库模板。
$output=apply_filters('post_gallery','','$attr);
如果($output!='')
返回$output;
//我们信任作者输入,所以至少要确保它看起来像一个有效的orderby语句
如果(isset($attr['orderby'])){
$attr['orderby']=sanitize_sql_orderby($attr['orderby']);
如果(!$attr['orderby'])
未设置($attr['orderby']);
}
提取(短码)附件(数组)(
“订单”=>“ASC”,
'orderby'=>'菜单\u订单ID',
'id'=>$post->id,
'itemtag'=>'dl',
'icontag'=>'dt',
'captiontag'=>'dd',
“列”=>3,
'大小'=>'中等',
'包括'=>'',
“排除”=>“
)美元(attr);;
$id=intval($id);
如果('RAND'=$order)
$orderby='none';
如果(!空($include)){
$\u attachments=get\u posts(数组('include'=>$include',post\u status'=>'inherit','post\u type'=>'attachment','post\u mime\u type'=>'image','order'=>$order',orderby'=>$orderby));
$attachments=array();
foreach($\附件为$key=>$val){
$attachments[$val->ID]=$\附件[$key];
}
}elseif(!empty($exclude)){
$attachments=get_children(数组('post_parent'=>$id,'exclude'=>$exclude,'post_status'=>'inherit','post_type'=>'attachment','post_mime_type'=>'image','order'=>$order',orderby'=>$orderby));
}否则{
$attachments=get_children(数组('post_parent'=>$id,'post_status'=>'inherit','post_type'=>'attachments','post_mime_type'=>'image','order'=>$order',orderby'=>$orderby));
}
如果(空($附件))
返回“”;
if(is_feed()){
$output=“\n”;
foreach($att_id=>$attachment形式的附件)
$output.=wp\u get\u attachment\u link($att\u id,$size,true)。“\n”;
返回$output;
}
$itemtag=tag_escape($itemtag);
$captiontag=tag_escape($captiontag);
$icontag=tag_escape($icontag);
$valid_tags=wp_kses_allowed_html('post');
如果(!isset($valid_tags[$itemtag]))
$itemtag='dl';
如果(!isset($valid_tags[$captiontag]))
$captiontag='dd';
如果(!isset($valid_tags[$icontag]))
$icontag='dt';
$columns=intval($columns);
$itemwidth=$columns>0?楼层(100/$columns):100;
$float=is_rtl()?“right”:“left”;
$selector=“gallery-{$instance}”;
$gallery\u style=$gallery\u div='';
$size\u class=sanitize\u html\u class($size);
$gallery_div=“”;
$output=apply_过滤器('gallery_style',$gallery_style.\n\t\t.$gallery_div);
$i=0;
foreach($id=>$attachment的附件){
$link=isset($attr['link'])和&'file'=$attr['link']?wp_获取附件链接($id,$size,false,false):wp_获取附件链接($id,$size,true,false);
$output.=“”;
$output.=”
$link
";
如果($captiontag&&trim($attachment->post_摘录)){
$output.=”
“.wptexturize($attachment->post_摘录)。”
";
}
$output.=“”;
}
$output.=”
\n”;
返回$output;
}

是否有人可以帮助确保无论链接到什么,所有大小的库缩略图像都不带尺寸显示

要制作一个反应灵敏的画廊,你真的不需要从IMG上删除所有的身高和体重标签。你所需要做的就是给你的图像标签添加一些css

例如:

<div class="facebook">
  <img src="http:///" height="100" width="100" >

如果在使用古腾堡博客编辑器添加图像时出现问题,请在css文件中使用此css自动设置宽度和高度,这样就没有问题了:

figure > img {
width: auto;
height: auto;}

:-)

尝试在
remove\img\u width\u height
函数的
$html
中添加一些内容来调试它,看看它是否有效。你能澄清一下吗?您的建议是什么?将preg\u replace的结果保存在$html中(这在
函数remove\u img\u width\u height
中)。出于调试目的,您可以执行
$html'测试'在第9行,在结束括号
}
之前,看看条件语句是否导致了trueI不是wordpress鉴赏家或其他什么,当你不确定是否执行了一个块时,这些是一些基本的实践。谢谢,但这不起作用。图像的纵横比不尽相同。它们必须受到高度或宽度的限制——以较大者为准。
figure > img {
width: auto;
height: auto;}