简体   繁体   English

我如何检索和显示 wordpress 中图像的替代文本?

[英]how do i retrieve and display the alt text of an image in wordpress?

how do i retrieve and display the alt text of an image in wordpress?我如何检索和显示 wordpress 中图像的替代文本? trying to substitute the title for the alt.试图用标题代替 alt。 here is the original code.这是原始代码。

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

so in the h1 i want:所以在 h1 中我想要:

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

tried putting it in a variable like so:尝试将其放入这样的变量中:

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

but it doesn't show up.但它没有出现。 any solutions?任何解决方案?

First of all you need to get attachment ID for getting alt text..首先,您需要获取attachment ID以获取alt文本。

Use this code to get that,使用此代码来获取,

$img_id = get_post_thumbnail_id(get_the_ID());

Now add your code,现在添加您的代码,

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

Make sure that, your attachment Alternative Text is not empty...确保您的attachment Alternative Text不为空...

You can add Alternative Text from wp-admin --> Media --> edit your attachment --> Alternative Text ...您可以添加Alternative Textwp-admin --> Media --> edit your attachment --> Alternative Text ...

Can you please try below code:你能试试下面的代码吗:

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

I found a script, try if this works for you.我找到了一个脚本,试试看是否适合你。 Add this in your theme's functions.php file在你的主题的functions.php 文件中添加这个

function isa_add_img_title( $attr, $attachment = null ) {函数 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 ); } add_filter( 'wp_get_attachment_image_attributes','isa_add_img_title', 10, 2 );

I found this script working on my end, similar from Mukesh Panchal.我发现这个脚本对我有用,类似于 Mukesh Panchal。 I've tried other script from other but it didn't work.我尝试过其他脚本,但没有奏效。

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

The code below fixes this.下面的代码解决了这个问题。 When a page loads, it finds all of the images that are missing alt text and looks to see if you've specified an alt text for it in the Media Library.当页面加载时,它会找到所有缺少替代文本的图像,并查看您是否在媒体库中为其指定了替代文本。 If so, it updates the image before loading the page.如果是这样,它会在加载页面之前更新图像。

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 );
<?php $image_id = get_post_thumbnail_id();
$image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', TRUE);
$image_title = get_the_title($image_id);?>
<img alt="<?php echo $image_title;?>">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM