Wordpress PHP echo alt文本

时间:2017-05-31 10:52:29

标签: php wordpress image alt

我使用WP模板http://wpshower.com/themes/expositio/

构建了一个简单的网站

该网站是:Mathiaswarnich.dk

没关系,但它没有显示alt文本,我无法想出php设置。这是图像的代码:

 * The template for displaying image attachments

// Retrieve attachment metadata.
$metadata = wp_get_attachment_metadata();

get_header();
?>

<section id="primary" class="content-area image-attachment">
    <div id="content" class="site-content" role="main">
        <header class="entry-header">
            <?php the_title('<h1 class="entry-title">', '</h1>'); ?>
            <div class="entry-meta">
                <div class="full-size-link">
                    <a href="<?php echo wp_get_attachment_url(); ?>"><?php echo $metadata['width']; ?> &times; <?php echo $metadata['height']; ?></a>
                </div>
                <div class="parent-post-link">
                    <a href="<?php echo get_permalink($post->post_parent); ?>" rel="gallery"><?php echo get_the_title($post->post_parent); ?></a>
                </div>
            </div>
        </header><!--
        --><article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <div class="entry-content">
                <div class="entry-attachment">
                    <div class="attachment">
                        <?php expositio_the_attached_image(); ?>
                    </div><!-- .attachment -->
<?php if (has_excerpt()) : ?>
                    <div class="entry-caption">
                        <?php the_excerpt(); ?>
                    </div><!-- .entry-caption -->
<?php endif; ?>
                </div><!-- .entry-attachment -->
                <?php
                the_content();
                wp_link_pages(array(
                    'before' => '<div class="page-links"><span class="page-links-title">'.__('Pages:', 'expositio').'</span>',
                    'after' => '</div>',
                    'link_before' => '<span>',
                    'link_after' => '</span>',
                ));
                ?>

2 个答案:

答案 0 :(得分:1)

是的,我最近在我的摄影网站上也发现了同样的问题,也使用了WP Expositio主题。

Stender的答案中引入的变通方法不起作用,因为图库由自定义函数处理。

解决方案非常简单:

第1步:在主题目录中打开/inc/template-tags.php。

第2步:转到第87行并在此处检查代码:

function expositio_image_tag($thumbnail_id, $size = 'post-thumbnail') {
$meta = wp_get_attachment_metadata($thumbnail_id);
if ($meta === false || $meta === '') {
    return sprintf(
        '<img src="%s" width="%s" height="%s" alt="" />',
        wpShower::defaultImage(),
        wpShower::$default_image_width,
        wpShower::$default_image_height
    );
}

$src = wp_get_attachment_image_src($thumbnail_id, $size);
$size_available = isset($meta['sizes'][$size]);
return sprintf(
    '<img src="%s" width="%s" height="%s" alt="" />',
    $src[0],
    $size_available ? $meta['sizes'][$size]['width'] : $meta['width'],
    $size_available ? $meta['sizes'][$size]['height'] : $meta['height']
);
}

第3步:将以上代码块替换为:

function expositio_image_tag($thumbnail_id, $size = 'post-thumbnail') {
$meta = wp_get_attachment_metadata($thumbnail_id);
if ($meta === false || $meta === '') {
    return sprintf(
        '<img src="%s" width="%s" height="%s" alt="" />',
        wpShower::defaultImage(),
        wpShower::$default_image_width,
        wpShower::$default_image_height
    );
}
$alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true);
$src = wp_get_attachment_image_src($thumbnail_id, $size);
$size_available = isset($meta['sizes'][$size]);
return sprintf(
    '<img src="%s" width="%s" height="%s" alt="%s" />',
    $src[0],
    $size_available ? $meta['sizes'][$size]['width'] : $meta['width'],
    $size_available ? $meta['sizes'][$size]['height'] : $meta['height'],
    $alt
);
}

第4步:保存/上传已编辑的文件。刷新网站。

基本上,我们在这里创建一个$ alt变量,它检索图像alt属性并将其添加到格式化的字符串值数组中。享受。

答案 1 :(得分:0)

如果是精选图片 - 您可以更改

 <?php expositio_the_attached_image(); ?> 

到这个

//full is the size - If you have a size from the theme, use that instead.
//don't know how your theme works, but maybe 'expositio_attachment_size'
<?php the_post_thumbnail('full'); ?>

甚至是这个

<div class="attachment">
      <?php expositio_the_attached_image(); ?>
</div><!-- .attachment -->

到此

<?php if ( has_post_thumbnail() ) : ?>
    <div class="attachment">
        <?php the_post_thumbnail('full'); ?>
    </div><!-- .attachment -->
<?php endif; ?>

如果您缺少某些图像类,也可以向此函数添加属性: 像这样

<?php if ( has_post_thumbnail() ) : ?>
    <div class="attachment">
        <?php the_post_thumbnail('full', ['class' => 'img-responsive responsive--full', 'title' => 'Feature image']); ?>
    </div><!-- .attachment -->
<?php endif; ?>
祝你好运,玩得开心!