正在为_POST_THMBIAN()的语法而苦苦挣扎;

时间:2012-03-23 作者:Nohl

我想显示特征图像的alt文本。

我一直在使用the\\u post\\u缩略图();用于显示图像的函数

像这样:

if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
    the_post_thumbnail(\'medium\');
} 
问题:如何显示此图像的Alt属性?

3 个回复
SO网友:Mamaduka

the_post_thumbnail() 接受第二个参数$attr, 这是一个选项数组,所以要显示图像的alt,请使用以下内容:

 the_post_thumbnail( \'medium\', array( \'alt\' => \'Image alt\' ) );

SO网友:Michael

我不久前尝试过的东西-纯粹是实验性的,没有经过广泛测试-这将把缩略图和字幕包装到一个类似于wp字幕图像的div中:(这个过滤函数需要添加到主题的functions.php中)

// 27/01/2012 alchymyth
// show thumbnail with caption, if it is entered, wrapped with <div class="wp-caption thumb-caption"> ;
// show just the thumbnail otherwise
add_filter(\'post_thumbnail_html\',\'add_post_thumbnail_caption\',10,5);
function add_post_thumbnail_caption($html, $post_id, $post_thumbnail_id, $size, $attr) {        
if( $html == \'\' ) { return $html;
} else {
 $out = \'\';

  $thumbnail_image = get_posts(array(\'p\' => $post_thumbnail_id, \'post_type\' => \'attachment\'));

  if ($thumbnail_image && isset($thumbnail_image[0])) {

  $image = wp_get_attachment_image_src($post_thumbnail_id, $size); 
  $t_width = $image[1] +0; // +0 here for no extra padding, needs to be considered in writing css - the default image caption uses +10;
  $class = $attr[\'class\'];
    if($thumbnail_image[0]->post_excerpt) $out .= \'<div class="wp-caption thumb-caption \'.$class.\'" style="width:\'.$t_width.\'px; ">\';

    $out .= $html; 

    if($thumbnail_image[0]->post_excerpt) $out .= \'<p class="wp-caption-text">\'.$thumbnail_image[0]->post_excerpt.\'</p></div>\'; 
  }
return $out; 
}
/*css classes for this:
.wp-caption.thumb-caption { }
.wp-caption.thumb-caption img { }
.wp-caption.thumb-caption .wp-caption-text { }
*/
}
输出示例:

<div class="wp-caption thumb-caption " style="width:165px; "><img width="165" height="150" src="http://localhost/wordpress/wp-content/uploads/2010/05/birthday_cake-180x163.jpg" class="attachment-580x150 wp-post-image" alt="happy birthday" title="birthday_cake" /><p class="wp-caption-text">happy birthday</p></div>

SO网友:Nohl

这就是我最后在我的页面上使用的内容。php

它由php操作符括起来。我正在使用css格式化它。

这似乎有点太简单了。我错过了什么?

if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
    echo \'<div class="featured-image">\';
    the_post_thumbnail(\'medium\');
    echo \'<span id="caption">\' . get_post( get_post_thumbnail_id() )->post_excerpt . \'</span>\';
    echo \'</div>\';
} 

结束

相关推荐

Functions file mods and CPU

将添加类似于我在下面粘贴到函数的代码。php主题文件会降低Wordpress站点的速度,还是影响CPU?(谢谢)function remove_menu_items() { global $menu; $restricted = array(__(\'Links\'), __(\'Comments\'), __(\'Media\'), __(\'Plugins\'), __(\'Tools\'), __(\'Users\')); end ($menu);&#