2017-05-31 50 views
0

我已經使用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)); –

回答

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

祝你好運,玩得開心!

+0

那麼這對你來說是如何實現的? – Stender

0

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

歡迎來到堆棧溢出!社區可能會認爲特定產品/資源的過度宣傳爲**垃圾郵件**。看看[幫助],特別是[用戶期望什麼樣的行爲?](// stackoverflow.com/help/behavior)的最後一節:_避免公開自我推銷_。您可能也對[如何在堆棧溢出做廣告?](// stackoverflow.com/help/advertising)感興趣。 –

+0

放置鏈接與我的答案嚴格相關,解決方案已解決。 –