get_post_field – Title without paragraph

Normally I wouldn’t insert a single post with a technique like this, but sadly this project requires it.

I try to call the post_title with:

echo apply_filters( 'the_content', get_post_field( 'post_title', $post_id ) );

The problem is, that this code will display the title inside a paragraph like:

<p>Title</p>

How can I block it out, or even replace it with an h2?

1 Answer
1

As per the discussion

the_content is not a proper hook for filtering post titles while it filters the post content. wpautop() function is called with the_content so it adds the necessary paragraphs.

To filter post titles use the_title instead, it won’t add any markup tags by default:

echo apply_filters('the_title', get_post_field('post_title', $post_id));

Leave a Comment