最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Echoing a variable comes out with lots of white space

programmeradmin3浏览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       " 

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 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 Commented 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(). – Jacob Peattie Commented Jul 2, 2020 at 5:36
Add a comment  | 

1 Answer 1

Reset to default 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

发布评论

评论列表(0)

  1. 暂无评论