2016-02-12 54 views
0

我想添加alt属性以在我的博客上张贴缩略图。将图像添加到WP后缩略图

我得到的alt文本回声,但不是作为一个属性,但作为文本!

<?php if (has_post_thumbnail()) {$image_src = wp_get_attachment_image_src(get_post_thumbnail_id(),’thumbnail’); $image_alt = wpseoFocusKW(); 
    echo '<img width="100%" src="' . $image_src[0] . '" alt=' . $image_alt .' >';} ?></div></div> 

你可以看到这里的问题:http://benefacto.org/three-days-paid-volunteering-leave-an-update-from-rob-wilsons-office/

你会注意到我使用的Yoast关键字为ALT,工作正常。

任何想法非常赞赏。

+2

你缺少了'“''上的属性alt'感谢 –

+0

@sebastianbrosch我更新了它,以包括“”,但它没有任何区别,奇怪! –

+0

你也使用back ticks而不是单引号'thumbnail'周围 – Und3rTow

回答

1

请尝试以下(仅PHP部分):

<?php 
    if (has_post_thumbnail()) { 
     $image_src = wp_get_attachment_image_src(get_post_thumbnail_id(),'thumbnail'); 
     $image_alt = wpseo_get_value('focuskw', $post->ID); 
     echo '<img width="100%" src="'.$image_src[0].'" alt="'.$image_alt.'">'; 
    } 
?> 

功能wpseoFocusKW()的内容是这样的:

function wpseoFocusKW() 
{ 
    $focuskw = wpseo_get_value('focuskw', $post->ID); 
    echo $focuskw; 
} 

此功能只能随声附和关键字,但不要回报!
参考:http://snipplr.com/view/67931/

您可以创建一个自定义函数或改变原这样的:

function wpCustomSeoFocusKW($return = false) 
{ 
    $focuskw = wpseo_get_value('focuskw', $post->ID); 

    if ($return) { 
     return $focuskw; 
    } else { 
     echo $focuskw; 
    } 
} 
+0

This Works!非常感谢塞巴斯蒂安。让它回归而不是回声的关键是什么? –

+0

用'return $ focuskw;'替换函数上的'echo $ focuskw;'或创建自己的自定义函数。 –

相关问题