如何将php添加到主题以显示alt属性

时间:2018-11-11 作者:5 Star Cruises

我正试图找出如何将PHP/代码添加到模板中,以便在图像中显示alt标记。这个主题已经被一家不再存在的公司修改了,所以我不能问如何纠正这个问题。

这是主题开发人员给我的代码,但它与主题附带的文件不对应。

<?php echo \'<img src="\' . $src[0] . \'" alt="\' . get_the_title() . \'" />\'; ?>
我想我的问题是,我如何使这项工作,使我的alt标记显示出来。

这是网站https://www.emabarkboathire.com.au 这是主要的修改页面。https://www.embarkboathire.com.au/boats/ https://www.embarkboathire.com.au/boats/galaxy/

我没有任何PHP经验,但知道如何使用Wordpress ok。

我们将非常感激您的帮助。

谢谢

丹尼

<div class="ycbe-gallery ycbe-new">
    <span class="ycbe-new-to-upload"><?php _e( \'To be uploaded on save:\', \'yachtcharter\' ); ?></span>
    <br clear="all">
</div>

<div class="ycbe-gallery ycbe-add-attachments">
    <a id="ycbe-add-attachments" class="ycbe-add-attachments"><?php _e( \'Add to gallery\', \'yachtcharter\' ); ?></a>
</div>

<input type="hidden" name="ycbe_gallery_hidden" id="ycbe-gallery-hidden" value="<?php echo $value; ?>">

<script type="text/javascript">
    jQuery( document ).ready( function( $ ) {
        var _custom_media = true,
        _orig_send_attachment = wp.media.editor.send.attachment;
        $( \'#ycbe-add-attachments\' ).click( function( e ) {
            var send_attachment_bkp = wp.media.editor.send.attachment;
            var button = $(this);
            var id = button.attr( \'class\' ).replace( \'_button\', \'\' );
            _custom_media = true;
            wp.media.editor.send.attachment = function( props, attachment ){
                if ( _custom_media ) {
                    if( attachment.type == \'image\' && attachment.subtype !== \'svg+xml\' ) {
                        if($( \'#ycbe-gallery-hidden\' ).val() == \'\') {
                            $( \'#ycbe-gallery-hidden\' ).val(attachment.id);
                        } else {
                            oldVal = $( \'#ycbe-gallery-hidden\' ).val();
                            $( \'#ycbe-gallery-hidden\' ).val( oldVal + \',\' + attachment.id );
                        }
                        var src = attachment.sizes.thumbnail.url;
                        $( \'.ycbe-gallery.ycbe-new\' ).show();
                        $( \'.ycbe-gallery.ycbe-new span\' ).after( \'<div class="ycbe-attachment"><img width="64" height="64" src="\' + src + \'" data-id="\' + attachment.id + \'" class="ycbe-attached-file ycbe-image" /><a class="ycbe-remove-attachment" data-id="\' + attachment.id + \'">Remove</a></div>\' );
                    }
                } else {
                    return _orig_send_attachment.apply( this, [props, attachment] );
                };
            }
            wp.media.editor.open(button);
            return false;

        });

        $( \'.add_media\' ).on( \'click\', function() {
            _custom_media = false;
        });

        $( \'.ycbe-remove-attachment\' ).live( \'click\', function() {
            if( confirm( \'Are you sure you want to remove this attachment?\' ) ) {
                valArr = $( \'#ycbe-gallery-hidden\' ).val().split( \',\' );
                var index = valArr.indexOf( $( this ).attr( \'data-id\' ) );
                if ( index > -1 ) {
                    valArr.splice( index, 1 );
                    $( this ).parent().remove();
                }
                $( \'#ycbe-gallery-hidden\' ).val( valArr.toString() );
            }
        });

    });
</script>

1 个回复
SO网友:Md. Ehsanul Haque Kanan

尝试将这些代码添加到主题中function.php 文件:

function isa_add_img_title( $attr, $attachment = null ) {

    $img_title = trim( strip_tags( $attachment->post_title ) );

    $attr[\'title\'] = $img_title;
    $attr[\'alt\'] = $img_title;

    return $attr;
}
add_filter( \'wp_get_attachment_image_attributes\',\'isa_add_img_title\', 10, 2 );

结束

相关推荐

https images not displaying

Setup嗨,伙计们!我有个奇怪的问题。我正在使用安装了ssl证书的wordpress Premium BeTheme。Problem问题是,虽然图像是用https上传到网站上的,但它们没有显示出来。如果我手动将图像的URL从https切换到http,它们就会开始显示。What I have done?我尝试过在internet上卸载ssl证书和几乎所有的解决方案,但都没有解决。Here is the URL of the website : https://uptimeelite.com/