2017-01-05 62 views
0

我打電話圖像轉換成single.php用下面的代碼添加CSS全元添加到WordPress的圖像

的single.php

<? if(has_post_thumbnail($post_id)): ?> 
    <div class="post-image"> 
     <img src="<?=wp_get_attachment_url(get_post_thumbnail_id()); ?>"> 
    </div> 
<? endif; ?> 

的問題是,我現在發現,元數據當我點擊編輯媒體沒有顯示時,通過CMS端添加。

有誰知道如何將它們編碼到我的single.php中?

我完全的single.php

<?php get_header(); ?> 

<? if(has_post_thumbnail($post_id)): ?> 
    <div class="single-featured-image post-image"> 
     <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1> 
     <img src="<?=wp_get_attachment_url(get_post_thumbnail_id()); ?>"> 
    </div> 
<? endif; ?> 

<img src="<?php echo $url; ?>" /> 

    <div class="blog-post"> 
      <?php 
      if (have_posts()) : while (have_posts()) : the_post(); 
       get_template_part('content-single', get_post_format()); 

       if (comments_open() || get_comments_number()) : 
        comments_template(); 
       endif; 

      endwhile; endif; 
      ?> 
    </div> 
</div> // closes of container 

<?php get_footer(); ?> 
+0

你想要展示什麼「元」?是不是你失去了它,而是你根本沒有顯示。 – yivi

+0

標題說明和alt –

回答

0

要獲得「alt」屬性,您需要使用get_post_meta作爲附件標識。但對於標題標題,您需要直接檢索附件帖子,因爲該信息存儲在wp_posts中。

所以你可以做這樣的事情:

<?php if(has_post_thumbnail($post_id)): 
    $pic_id  = get_post_thumbnail_id(); 
    $pic   = get_post($pic_id); 
    $caption  = $pic->post_excerpt; 
    $image_alt = get_post_meta($pic->ID, '_wp_attachment_image_alt', true); 
    $image_title = $pic->post_title; 
    ?> 
    <div class="post-image"> 
     <h4><?php echo $image_title; ?></h4> 
     <img src="<?php echo wp_get_attachment_url($pic_id); ?>" alt="<?php echo $image_alt; ?>"> 
     <span class="description"><?php echo $caption;?></span> 
    </div> 
<? endif; ?> 

更改HTML,以滿足您的需求,我不知道你想顯示圖例和標題。

+0

嗯,感謝提供錯誤atm生病調試,並得到您 –

+0

加1 il檢查出來。仍然出現錯誤,但將保留在它 –

+0

是的,抱起病後我得到什麼 –

0

你可能需要的是get_post_meta。例如:

$meta_value = get_post_meta(get_the_ID(), 'meta_key_name', true); 

get_the_ID()將是當前帖子的標識。用我們自己的元鍵替換meta_key_name