How to display the content for given page ID withot the <p></p> wrapper?

How to display the content for given page ID withot the <p></p> wrapper?

The method I’m currently using is:

Read More
<?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.

Related posts

Leave a Reply

2 comments

  1. 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); ?>
    
  2. 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;  
    ?>