guynumerouno
guynumerouno

Reputation: 217

Get post_content as a string

I have an array of posts from an WPQuery by doing

$query = new WPQuery(.....)
$array_of_posts = $query->post_content

However this is returning the content as HTML as with some other stuff too

<!-- wp:paragraph -->
<p>the text</p>
<!-- /wp:paragraph -->

And then i want to display this as content for each post in the array

foreach($array_of_posts as $post){
  <h1> echo $post->post_content </h1>

But of course this just gives me a h1 with the html and other stuff. How can i just get the string?

Also this code is just pseudo code i know the syntax is wrong

Upvotes: 2

Views: 1428

Answers (1)

amarinediary
amarinediary

Reputation: 5441


You're looking wp_strip_all_tags()

Properly strip all HTML tags including script and style. This differs from strip_tags() because it removes the contents of the <script> and <style> tags. E.g. strip_tags( '<script>something</script>' ) will return ‘something’. wp_strip_all_tags will return ‘’

echo wp_strip_all_tags( the_content() );

Alternatively , you could use remove_filter('term_description','wpautop'); in your function.php to remove the <p> tags.

Upvotes: 1

Related Questions