WordPress的PHP回声替代文字

问题描述:

我已经使用WP模板建立一个简单的网站http://wpshower.com/themes/expositio/WordPress的PHP回声替代文字

该网站是: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>', 
       )); 
       ?> 
+0

你在哪里打印ALT? –

+0

你必须找到这个函数'expositio_the_attached_image();'定义和修改的位置(如果你自己没有设法执行它,可能会显示它的代码和文件名) – Kaddath

+0

找到它: \t function expositio_the_attached_image() { \t \t $ post = get_post(); \t \t */ \t \t $ attachment_size = apply_filters('expositio_attachment_size',array(810,810)); \t \t $ next_attachment_url = wp_get_attachment_url(); \t \t $ attachment_ids = get_posts(阵列( \t \t \t 'post_parent'=> $后> post_parent, \t \t \t '字段'=> 'IDS', \t \t \t 'numberposts'=> -1, \t \t \t 'post_status'=> '继承', \t \t \t 'post_type'=> '附着', \t \t \t 'post_mime_type'=> '图像', \t \t \t '顺序'=> 'ASC', \t \t \t '的OrderBy'=> 'menu_order ID', \t \t)); –

,如果它是一个有特色的形象 - 你可以改变

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

祝你好运,玩得开心!

+0

那么这对你来说是如何实现的? – Stender

是的,我最近也在使用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

欢迎来到堆栈溢出!社区可能会认为特定产品/资源的过度宣传为**垃圾邮件**。看看[帮助],特别是[用户期望什么样的行为?](// *.com/help/behavior)的最后一节:_避免公开自我推销_。您可能也对[如何在堆栈溢出做广告?](// *.com/help/advertising)感兴趣。 –

+0

放置链接与我的答案严格相关,解决方案已解决。 –