0

I'm trying to display images with their caption, but when I do it displays both the caption and that alt text. I've tried to use unset( $fields['image_alt'] ); to hide the alt text, but it does nothing. Here's my code:

                    <?php if (the_post_thumbnail_caption()) { 
                         the_post_thumbnail_caption();                          
                        } 

                        function remove_alt( $fields ) {
                            unset( $fields['image_alt'] );
                            return $fields;
                        }
                        add_filter( 'attachment_fields_to_edit', 'remove_alt', 999, 1 );
                    ?>

It first gets the caption, if there is one, and then the function is supposed to remove the alt text and keep the caption, but it's not working. Any suggestions for how to get this to work? Thanks so much for the help :)

1 Answer 1

0
echo wp_get_attachment_caption(get_post_thumbnail_id());
2
  • Hey jxxe, thanks for the response. This actually does the same thing as the if statement at the beginning of my code. I'm trying to display the caption without the alt text.
    – frinci
    Commented May 4, 2020 at 15:51
  • I'm confused, because both my solution and the_post_thumbnail_caption() return just the caption for me: i.imgur.com/ycpn8eA.png. The post I tested it on has a featured image with a caption and alt text.
    – jxxe
    Commented May 4, 2020 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.