【问题标题】:how do i retrieve and display the alt text of an image in wordpress?如何在 wordpress 中检索和显示图像的替代文本?
【发布时间】:2014-03-23 14:53:17
【问题描述】:

如何在 wordpress 中检索和显示图像的替代文本?试图用标题代替 alt。这是原始代码。

<?php while ( have_posts() ) : the_post(); ?>

  <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
   <header class="entry-header">
    <h1 class="entry-title"><?php the_title(); ?></h1>

所以在我想要的 h1 中:

<h1 class="entry-title">"alt text of image"</h1>

试着把它放在一个像这样的变量中:

 $alt_text = get_post_meta($img_id , '_wp_attachment_image_alt', true);
<h1 class="entry-title"><?php echo $alt_text; ?></h1>

但它没有显示出来。有什么解决办法吗?

【问题讨论】:

  • 当前输出是多少?你确定变量$img_id 是正确的吗?
  • 当前输出什么都不是。不知道 $img_id 是否正确。

标签: php wordpress image alt


【解决方案1】:

首先,您需要获得attachment ID 才能获得alt 文本..

使用此代码获取,

$img_id = get_post_thumbnail_id(get_the_ID());

现在添加您的代码,

<?php $alt_text = get_post_meta($img_id , '_wp_attachment_image_alt', true); ?>
<h1 class="entry-title"><?php echo $alt_text; ?></h1>

确保您的attachment Alternative Text 不为空...

你可以从wp-admin --&gt; Media --&gt; edit your attachment --&gt; Alternative Text添加Alternative Text...

【讨论】:

  • &lt;?php get_header(); ?&gt; &lt;div id="primary" class="content-area image-attachment"&gt; &lt;div id="content" class="site-content" role="main"&gt; &lt;?php while ( have_posts() ) : the_post(); ?&gt; &lt;?php $img_id = get_post_thumbnail_id(get_the_ID()); $alt_text = get_post_meta($img_id , '_wp_attachment_image_alt', true); ?&gt; &lt;article id="post-&lt;?php the_ID(); ?&gt;" &lt;?php post_class(); ?&gt;&gt; &lt;header class="entry-header"&gt; &lt;h1 class="entry-title"&gt;&lt;?php echo $alt_text; ?&gt;&lt;/h1&gt;
【解决方案2】:

你能试试下面的代码吗:

<?php
$thumb_id = get_post_thumbnail_id(get_the_ID());
$alt = get_post_meta($thumb_id, '_wp_attachment_image_alt', true);
if( $alt ):
    echo $alt;
endif;
?>

【讨论】:

    【解决方案3】:

    我找到了一个脚本,试试它是否适合你。将此添加到主题的 functions.php 文件中

    function isa_add_img_title($attr, $attachment = null) {

    $img_title = trim( strip_tags( $attachment->post_title ) );
    
    $attr['title'] = $img_title;
    $attr['alt'] = $img_title;
    
    return $attr;
    

    } add_filter('wp_get_attachment_image_attributes','isa_add_img_title', 10, 2);

    【讨论】:

      【解决方案4】:

      我发现这个脚本对我来说很有效,类似于 Mukesh Panchal。我尝试了其他脚本,但没有成功。

      <?php $thumb_id = get_post_thumbnail_id(get_the_ID()); 
      $alt = get_post_meta($thumb_id, '_wp_attachment_image_alt', true); ?>
      <img class="image" src="<?php echo $feature_image; ?>" alt="<?php echo $alt; ?>" >
      

      【讨论】:

        【解决方案5】:

        下面的代码解决了这个问题。当页面加载时,它会查找所有缺少替代文本的图像,并查看您是否在媒体库中为其指定了替代文本。如果是这样,它会在加载页面之前更新图像。

        add_filter( 'render_block', function( $content, $block ) {
            if( 'core/image' !== $block['blockName'] )
                return $content;
        
            $alt = get_post_meta( $block['attrs']['id'], '_wp_attachment_image_alt', true );
            if( empty( $alt ) )
                return $content;
        
            // Empty alt
            if( false !== strpos( $content, 'alt=""' ) ) {
                $content = str_replace( 'alt=""', 'alt="' . $alt . '"', $content );
        
            // No alt
            } elseif( false === strpos( $content, 'alt="' ) ) {
                $content = str_replace( 'src="', 'alt="' . $alt . '" src="', $content );
            }
        
            return $content;
        }, 10, 2 );
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-11-14
          • 1970-01-01
          • 2017-09-28
          • 2017-03-22
          • 2017-09-21
          • 1970-01-01
          • 2019-02-11
          相关资源
          最近更新 更多