【问题标题】:Wordpress PHP echo alt textWordpress PHP 回显替代文本
【发布时间】:2017-11-01 03:25:08
【问题描述】:

我已经使用 WP 模板 http://wpshower.com/themes/expositio/ 构建了一个简单的网站

网站是:Mathiaswarnich.dk

没关系,但它不显示替代文本,我无法弄清楚 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>',
                ));
                ?>

【问题讨论】:

  • 你在哪里打印 alt?
  • 你必须找到这个函数 expositio_the_attached_image(); 的定义并修改它(如果你自己不能做到,可能会告诉我们它的代码和文件名)
  • 找到它:function expositio_the_attached_image() { $post = get_post(); */ $attachment_size = apply_filters('expositio_attachment_size', array(810, 810)); $next_attachment_url = wp_get_attachment_url(); $attachment_ids = get_posts(array( 'post_parent' => $post->post_parent, 'fields' => 'ids', 'numberposts' => -1, 'post_status' => 'inherit', 'post_type' => ' attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID', ));
  • 是您要显示的缩略图/特色图片吗?`

标签: php wordpress image alt


【解决方案1】:

如果是特色图片 - 您可以更改

 <?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; ?>

祝你好运,玩得开心!

【讨论】:

    【解决方案2】:

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

    Stender 的回答中介绍的解决方法将不起作用,因为图库是由自定义函数处理的。

    解决方法很简单:

    第一步:打开主题目录下的/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 属性并将其添加到格式化的字符串值数组中。享受吧。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多