scriptygoddess

27 Oct, 2009

Striping IMG tags from the_content in WordPress (and how to fudge page excerpts)

Posted by: Jennifer In: WordPress|WordPress Hacks|WordPress: Lessons Learned

Background: For a site I was working on, I was pulling in content from another page onto the homepage. However I wanted to just bring in an excerpt not the whole page. (And actually, a customized excerpt at that – not a default set character or word limit). I also didn't want any images brought in – I just wanted the text and any associated formatting.

Well, one problem here is that WordPress pages (at least as of this writing – with WordPress in version 2.8.5) do not have excerpts for pages. Only for posts. The way around this is to use the <!-- more --> tag where you want your break. So that works fine – but what about stripping out the images? I'm still working on my reg-ex knowledge, but I found this one from here and it worked for me. So this is what I'm doing when I'm pulling in my page excerpt:

<?php
$posts = query_posts('page_id=1234');
if (have_posts()) : while (have_posts()) : the_post();
//this makes the more work...
global $more; $more = 0;
?>
<h1><?php the_title(); ?></h1>
<?php
$content = get_the_content('');
$content = preg_replace('/<img[^>]+>/is', '', $content);
echo $content;
?>
<p><a href="<?php the_permalink(1234); ?>"><img src="/images/more.gif" /></a></p>
<?php endwhile; endif; ?>

*we're assuming "1234" is the ID of my page in the example above…

You'll also notice I manually added my "more" button image…

To read more about the more tag (and see where I got that global $more; $more = 0; from, you can see the WordPress codex here on the subject. (Scroll to the very bottom of the page – to the section title "How to use Read More in Pages")

Related posts:

  1. Search from WordPress Admin (Post Listing) Redirecting to Home Page This is possibly the most bizarre thing I've ever seen....
  2. Adding a block of HTML to a WordPress post One problem with the WYSIWYG editor in WordPress is that...
  3. Two Blocks of Content for Pages in WordPress I just saw this plugin show up on the WordPress...
  4. Anchor Links in WordPress Posts – another shortcode solution I was recently asked by a client how they could...

Related posts brought to you by Yet Another Related Posts Plugin.

3 Responses to "Striping IMG tags from the_content in WordPress (and how to fudge page excerpts)"

1 | Stephen R

November 1st, 2009 at 11:26 am

Avatar

Pages *can* have excerpts (they're just a different form of Post, after all); they just don't have the interface for it. Try this plugin: http://blog.ftwr.co.uk/wordpress/page-excerpt/

;-)

2 | Jennifer

November 1st, 2009 at 11:29 am

Avatar

I have seen that plugin – and I think I tried to use it for this project. For some reason I couldn't use it and unfortunately I can't remember why. Next time I have to do a similar setup, I will try it again and try to remember to make a note of what issue I ran into with it…

3 | Daniel Markham

November 27th, 2009 at 7:24 pm

Avatar

Thanks. This is awesome. It is quizzical that WordPress, by default, does not include a way to access/create page excerpts.

I used this in the footer of my blog. I a using Derek Powazek's "depo skinny" theme. I used this code to replace the code he included in the footer that pulled what ever you put in the description on the profile section in the WP admin. Works great.

Comment Form

Featured Sponsors

About


Advertisements