5

I'm currently developing a website with WordPress 3.5 and I need to retrieve Post Text (Only text, not include images) at Archive Page. I can retrieve it with wp_excerpt() method without any problems. But the main problem for me is that I can't get the exact text layout. wp_excerpt() method returns text which ignores all extra spaces and line breaks. What should I do? I think I will get Only Post Text with Exact Layout if I can retrieve from wp_content() method. Thanks in advance for your help!

9

Or even simpler:

echo wp_strip_all_tags( get_the_content() );

By using:

  • get_the_content()

    Retrieve the post content. (Must be used in a Loop)

    An important difference from the_content() is that get_the_content() does not pass the content through the 'the_content'. This means that get_the_content() will not auto-embed videos or expand shortcodes, among other things.

  • wp_strip_all_tags()

    Properly strip all HTML tags including script and style.

  • This should be approved as the answer. – AlbertSamuel Apr 8 '18 at 23:44
4

There's not a native WordPress function to retrieve text only, but you can use WordPress filters and regex code to target this specific problem.

To get unformated text, use get_the_content() function. To apply all filters, use it this way( see codex: http://codex.wordpress.org/Function_Reference/the_content#Alternative_Usage ):

$content = get_the_content();
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;

Before applying filters, there is a space for you custom modifications, e.g. removing images. This way:

$content = get_the_content();
$content = preg_replace('/(<)([img])(\w+)([^>]*>)/', "", $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]&gt;', $content);
echo $content;

Source of preg_replace code: http://www.phpzag.com/php-remove-image-tags-from-a-html-string-with-preg_replace/

You may need to remove also shortcodes, if any are used. This can be done also via preg_replace and I bet you'll find some on the google.

  • 1
    Thank you so much for your reply and also apologize for my late response. I tried like this. $content = get_the_content(); $content = preg_replace('/(<)([gallery])(\w+)([^>]*>)/', ”, $content); $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]&gt;', $content); echo $content; But it didn't work. No images are removed and can't see any text that I expect. – Thiha Maung Feb 24 '13 at 11:08
1

The following code works perfectly for me. Just add this to your functions.php file in your theme:

// Hook : to get content without images
add_filter('the_content', 'wpse_get_content_without_images');

function wpse_get_content_without_images() {
    $content = get_the_content();
    $content = preg_replace( '/<img[^>]+./', '', $content );
    echo $content;
}

Then, get your post content the standard way using echo the_content();.

1

I combined results from the other answers on this post to strip images and audio etc, but keeping the formatting. By first getting the content with get_the_content, then passing it through the "the_content" filter to add formatting etc, and then using php'sstrip_tags to only allow a limited number of tags.

strip_tags(apply_filters('the_content',get_the_content()),"<p><a><br><b><u><i><strong><span><div>");

0

Here is the code to remove gallery content and to have only content.

$content = get_the_content();
        $content = preg_replace('/\[gallery.*ids=.(.*).\]/', "", $content);
        $content = apply_filters('the_content', $content);
        $content = str_replace(']]>', ']]&gt;', $content);
        echo $content;
  • Isn't that exactly what some of the other answers have said only with less explanation? – Matthew Brown aka Lord Matt Sep 4 at 5:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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