remove-img-from-content
When working with WordPress development couple of days ago, I’ve manage a few quick Q&A with some of my colleague with user experience when dealing with WordPress posts. One of most sought out or talked topic was the_content() vs the_excerpt() features in WordPress post formatting.

Using the_excerpt() is great for user to shortened their long posts into 30-50 short text. However it doesn’t let you control where the post length should stop when converting to shorter text. The usual code used when using featured thumbnail with short text were like this:

<?php
the_post_thumbnail('thumbnail', array('class' => 'alignleft'));
the_excerpt();
?>

This will end result with:
thumb-with-excerpt

But this is not what user want sometime, they want to add their own ‘read more’ and want to control where to stop the long post and most important part is to preserve the HTML format. So we try this code instead:

<?php
the_post_thumbnail('thumbnail', array('class' => 'alignleft'));
the_content('...read more');
?>

This will end result with:
thumb-with-content
This may seem good enough but what if you do not want any images to show from the_content(). Notice the ‘red’ mark i added. This image from the post are not something user want to show in their short text.

In this WordPress Tutorial, I’ll show you how to remove images from the_content() but still preserve the HTML formatting in post. First insert below code into your theme functions.php

if( !function_exists('get_the_content_with_format') ):
function get_the_content_with_format ($more_link_text = '', $stripteaser = 0, $more_file = '') {
$content = get_the_content($more_link_text, $stripteaser, $more_file);
$content = apply_filters('the_content', $content);
$content = strip_tags($content, '<p><a>');
return $content;
}
endif;

Notice i only allow HTML tag ‘p’ and ‘a’ when the $content return as the_content().

Now instead of using the_excerpt() and the_content(), we used this code instead:

<?php
the_post_thumbnail('thumbnail', array('class' => 'alignleft'));
echo get_the_content_with_format($more_link_text = '...Read More'); 
?>

Final result will be like this:
strip-img-the-content

That’s about it folks, hope you find this tutorial useful and can help you in your next WordPress development project. Please enjoy and share this tutorial if you like it.

Photo credits to alex barth

How to Remove Images from the_content() but Preserve HTML Formatinghttp://cdn.dezzain.com/1/2013/11/remove-img-from-content.jpghttp://cdn.dezzain.com/1/2013/11/remove-img-from-content-300x296.jpg Richie KS WordPress Tutorials,
When working with WordPress development couple of days ago, I've manage a few quick Q&A with some of my colleague with user experience when dealing with WordPress posts. One of most sought out or talked topic was the_content() vs the_excerpt() features in WordPress post formatting. Using the_excerpt() is great...
<img src="http://cdn.dezzain.com/1/2013/11/remove-img-from-content.jpg" alt="remove-img-from-content" width="794" height="296" class="alignnone size-full wp-image-1525" /> When working with WordPress development couple of days ago, I've manage a few quick Q&A with some of my colleague with user experience when dealing with WordPress posts. One of most sought out or talked topic was <strong>the_content()</strong> vs <strong>the_excerpt()</strong> features in WordPress post formatting.<span id="more-1517"></span> Using <strong>the_excerpt()</strong> is great for user to shortened their long posts into 30-50 short text. However it doesn't let you control where the post length should stop when converting to shorter text. The usual code used when using featured thumbnail with short text were like this: <strong>This will end result with:</strong> <img src="http://cdn.dezzain.com/1/2013/11/thumb-with-excerpt.jpg" alt="thumb-with-excerpt" width="652" height="272" class="alignnone size-full wp-image-1518" /> But this is not what user want sometime, they want to add their own 'read more' and want to control where to stop the long post and most important part is to preserve the HTML format. So we try this code instead: <strong>This will end result with:</strong> <img src="http://cdn.dezzain.com/1/2013/11/thumb-with-content.jpg" alt="thumb-with-content" width="666" height="775" class="alignnone size-full wp-image-1520" /> This may seem good enough but what if you do not want any images to show from the_content(). Notice the 'red' mark i added. This image from the post are not something user want to show in their short text. In this WordPress Tutorial, I'll show you how to remove images from the_content() but still preserve the HTML formatting in post. First insert below code into your theme <strong>functions.php</strong> Notice i only allow HTML tag 'p' and 'a' when the $content return as <strong>the_content()</strong>. Now instead of using the_excerpt() and the_content(), we used this code instead: <strong>Final result will be like this:</strong> <img src="http://cdn.dezzain.com/1/2013/11/strip-img-the-content.jpg" alt="strip-img-the-content" width="670" height="376" class="alignnone size-full wp-image-1522" /> That's about it folks, hope you find this tutorial useful and can help you in your next WordPress development project. Please enjoy and share this tutorial if you like it. <small>Photo credits to <a href="http://www.flickr.com/photos/a-barth/3009812122/" target="_blank">alex barth</a></small>

You might also like to read:

WordPress Tutorials

Easier Retina Support for Your WordPress Themes

WordPress Tutorials

How to Show All Child Categories from Parent Category in WordPress

WordPress Tutorials

How to use WordPress Navigation Menus

Like Our Posts? Subscribes Now!

Preview on Feedage: dezzaincom

Add to My Yahoo! Dezzain.com

Add to Google! Dezzain.com

Add to AOL! Dezzain.com

Add to MSN Dezzain.com

Subscribe in NewsGator Online Dezzain.com

Add to Netvibes Dezzain.com

Subscribe in Pakeflakes Dezzain.com

Add to Windows Live Dezzain.com

iPing-it Dezzain.com

Add To Fwicki Dezzain.com