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 "
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 "
Share
Improve this question
edited Jul 1, 2020 at 19:53
mozboz
2,6281 gold badge12 silver badges23 bronze badges
asked Jul 1, 2020 at 18:39
dsaxdsax
1
2
|
1 Answer
Reset to default 1To 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
the_post_thumbnail()
outputs the alt text automatically. You should rarely need to manually get the text withget_post_meta()
. – Jacob Peattie Commented Jul 2, 2020 at 5:36