How to display the content for given page ID withot the <p></p>
wrapper?
The method I’m currently using is:
<?php
$id=21;
$post = get_page($id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
?>
For example page ID = 21
has the following content: Some content
What the wordpress echo is <p>Some content</p>
Any suggestion much appreciated.
You can add this in your functions.php file for instance,
Which will remove the paragraph formatting from
the_content
template tag in all cases.Should you have a need for it in the future, you can write;
<?php wpautop(the_content());?
> in your template to add formatting without having to add the filter back.Using
echo get_the_content();
will by pass the WordPresswpautop
filter too.Here’s a little function I wrote for you that you can use in your template files (paste the snippet into functions.php and see usage instructions below).
Usage:
Now some of this might be redundant but it illustrates how you can create a multipurpose function for use within your templates that can give you some, all or no formatting based upon your situation.
Note: In case anyone is wondering why
wpautop
is wrapped aroundget_the_content
above, is due toget_the_content
not being passed through thewpautop
filter, therefore if you want to use<p>
tags in a specified list of allowed tags whilst using the customnotags
argument you need to add<p>
formatting back withwpautop
.Alternative for getting the content of a page based on ID and without
<p>
tags;Usage:
You need to remove the
wpautop
filter ex: