0

I display my custom field like this :

<p><?php echo get_post_meta($post->ID, $my_field, true); ?></p>

As long as the field contains plain text, the markup is normal :

<p>something</p>

But if I put some HTML list in it, the content jumps out of the P tags and some additional P tags are even added :

<p></p>
<ul>
<li>something</li>
<li>something</li>
</ul>
<p></p>

Why is that and how to fix the problem ?

2
  • Is this just the output from your browser’s debugger or a real source code view?
    – fuxia
    May 7 '13 at 10:53
  • I see that in Firebug. In the browser I see that the custom field content jumped out of the P tags because there's a style (thick left border) on this tag and the content is below it.
    – drake035
    May 7 '13 at 11:23
1

Let wpautop() do the work:

echo wpautop( get_post_meta($post->ID, $my_field, true) );

wpautop() will detect leading block elements and use <p> only when the value starts with a string.

5
  • Same result : 46.20.119.207/~asuntosf/wordpress_test/?p=18 As long as I display the custom field between P tags, using wpautop or not, it will jump out of the P tags if it contains UL and LI elements.
    – drake035
    May 8 '13 at 10:49
  • 1
    @drake035 Note that my solution does not include manual p tags, let wpautop() add those.
    – fuxia
    May 8 '13 at 14:27
  • Thanks toscho. It works when I remove manual p tags. However it works without using wpautop() anyway as long as there's no manual p tags. Using div tags instead of p tags I don't have any problem. Still, this doesn't explain this weird behaviour : why does the content jump out of the p tags like that? I see no logical reason for this.
    – drake035
    May 9 '13 at 10:33
  • 1
    That happens because a p element cannot contain block elements. The browser has to close it to open the next block element.
    – fuxia
    May 9 '13 at 10:34
  • Hm I didn't know that, thanks a lot toscho.
    – drake035
    May 9 '13 at 11:21

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.