0

I'm having trouble when I get variables from WP function come out very long. How do I trim that down? trim and substr do not see the spaces before or after. Why is it so long?

<?php  $image_alt = trim(get_post_meta(get_post_thumbnail_id(), '_wp_attachment_image_alt', true)); ?>
            <?php echo $image_alt;?>

when I view the source it looks like this:

"

                        HIPAA compliant Times-2 X2 Rotating Shelving Rotating Cabinet Starter and Add-on       " 
2
  • 1
    I see you've a lot of opening and closing tags with lots of spaces and tabs inbetween, are you sure those aren't what you're seeing? This doesn't look like a WP problem but a general PHP issue
    – Tom J Nowell
    Jul 1, 2020 at 19:11
  • FWIW the_post_thumbnail() outputs the alt text automatically. You should rarely need to manually get the text with get_post_meta(). Jul 2, 2020 at 5:36

1 Answer 1

1

To expand on @Tom J Nowell's comment, any whitespace after ?> and before <?php will get output, so if you are adding an alt tag you need to make there there's no extra white space outside the <?php ?>. Here's an example, and I've simplified your code a bit as you can do that in one command:

<img src="..." alt="<?php echo trim(get_post_meta(get_post_thumbnail_id(), '_wp_attachment_image_alt', true)); ?>">

If you need more help it's useful to put the whole bit of code you're using in your question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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