Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

You can add this in your functions.php file for instance,

remove_filter ('the_content',  'wpautop');

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 WordPress wpautop 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).

function custom_content($args = '', $allow = ''){

if ($args == 'alltags') {

    $type = the_content();

} elseif ($args == 'notags') {

    $type = strip_tags(wpautop(get_the_content()), $allow);

} else {

    $type = get_the_content();

    }

echo $type;

}

Usage:

<?php custom_content();?> // no <p> tags, but <a>, <strong>, etc are preserved.
<?php custom_content('notags');?> // strips all tags from content
<?php custom_content('notags', '<a>');?> // strips all tags except <a> 
<?php custom_content('notags', '<a><p><etc..>');?> // strips all tags except <a> & <p> 
<?php custom_content('alltags');?> // all tags uses the_content();

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 around get_the_content above, is due to get_the_content not being passed through the wpautop filter, therefore if you want to use <p> tags in a specified list of allowed tags whilst using the custom notags argument you need to add <p> formatting back with wpautop.

Alternative for getting the content of a page based on ID and without <p> tags;

function get_content($id) {

    $post = get_page($id);
    $content = apply_filters('get_the_content', $post->post_content);
    echo $content;

} 

Usage:

<?php get_content(21); ?>
share|improve this answer
    
Hmm.. This does not seams to work. Does it make any difference that I'm running this on WAMP? –  User789 Jun 27 '12 at 16:47
    
Have you tried using get_the_content instead, this strips P –  userabuser Jun 27 '12 at 17:10
    
how do I use get_the_content and show page by ID at the same time, any code example please? –  User789 Jun 27 '12 at 18:55
1  
@NewUser See above for an update, additional snippet, that can be placed in your functions.php file and used in your template files. –  userabuser Jun 27 '12 at 19:22
add comment

You need to remove the wpautop filter ex:

<?php 
$id=21; 
$post = get_page($id); 
//remove auto p filter
remove_filter ('the_content','wpautop');
$content = apply_filters('the_content', $post->post_content); 
//return the filter for other posts/pages down the road
add_filter ('the_content','wpautop');
echo $content;  
?>
share|improve this answer
    
Please see here: dl.dropbox.com/u/17683975/stackoverlow/WordPress-Without-P.png // Does not seems to work. Does it matter that I'm runnning this on WAMP? –  User789 Jun 27 '12 at 16:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.