i've implemented a jQuery gallery for displaying several images within a post. the problem is that the_content(); also displays the post image. any ideas how to filter it? thanks

  • 1
    maybe you can give us a little more to work with? Are you just attaching the images to the post or are you actually putting them in the post content? – eileencodes Mar 1 '11 at 14:58
  • i've attached the images directly to the post (i'm having the [gallery] tag in my content) - but as attaching images has to be easy for our customer, the tag needs to remain in the content - that's why i need to filter it at the frontend. thanks – Fuxi Mar 1 '11 at 15:08
  • you would still attach images per post just leave out gallery short code and write a get_posts query for attachments. See codex.wordpress.org/Template_Tags/get_posts as the post_type would be attachments and then use wp_get_attachment_image (codex.wordpress.org/Function_Reference/wp_get_attachment_image) and manually build your gallery in the theme. The only difference would be the lack of [gallery] in your post content. – eileencodes Mar 1 '11 at 15:53

you can add a filter to the_content hook to strip the images

something like:

add_filter('the_content', 'strip_images',2);

function strip_images($content){
   return preg_replace('/<img[^>]+./','',$content);
  • is it possible to have this to work only on certain pages and not on other pages (or categories?) – alemur Apr 4 '12 at 14:40
  • Yep, just check in the function if its the right page using Conditional Tags codex.wordpress.org/Conditional_Tags – Bainternet Apr 4 '12 at 14:45

This is better:

<?php echo strip_shortcodes(wp_trim_words( get_the_content(), 80 )); ?>

Change 80 to whatever count words.

  • 5
    Please explain why that could solve the problem. – fuxia Mar 18 '13 at 23:08

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.