I struggled lot for this issue. Some times I only removed the first image. some times I removed first caption. Using following code you can easily remove the post first image and caption.
How to remove first image from wordpress post with caption if caption is present
I used the following code for first image removing.
/* * Removing the first image from post */ function remove_first_image ($content) { if (!is_page() && !is_feed() && !is_feed() && !is_home()) { if (preg_match("/<img[^>]+\>/i",$content)) { $content = preg_replace("/<img[^>]+\>/i", "", $content, 1); } return $content; } }
But I faced the some issues with that code. When first image has caption then only image get removed and caption will remain there.
After that I used the following code for caption remove.
/* * Removing the first caption from post with image */ function remove_first_image_caption ($content) { if (!is_page() && !is_feed() && !is_feed() && !is_home()) { $content = preg_replace("(\)", "", $content, 1); } return $content; } add_filter('the_content', 'remove_first_image_caption');
But if first image has no caption and second image image has caption then both the images will be get removed from post.
After some struggle I written following code. Following is the solution:
Final Code.
/* * Removing the first image from post * functionality added to delete caption is present to first image. */ function remove_first_image ($content) { if (!is_page() && !is_feed() && !is_feed() && !is_home()) { if (preg_match("/<img[^>]+\>/i",$content)) { //find first image URL $first_img = ''; $output = preg_match_all('/< *img[^>]*src *= *["\']?([^"\']*)/', $content, $matches); $first_img = $matches[1][0]; //find first image caption inner text $output = preg_match_all("/caption=['"](.*)/", $content, $matches); //find first image present in first caption text or not $pos = strpos($matches[0][0], $first_img); // if image URL found in caption array then delete the caption and image if ($pos !== false) { $content = preg_replace("(\)", "", $content, 1); } else { $content = preg_replace("/<img[^>]+\>/i", "", $content, 1); } } } return $content; } add_filter('the_content', 'remove_first_image');
solved: pagination for Custom post type not working
Some people asked me about pagination of custom post type. That is very easy.
For showing the custom post type we always use the query_post method.
Just use the following code in your template or theme file.
<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php get_header(); ?> <!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php query_posts( 'post_type=custom-post-type&paged='.$paged ); if (have_posts()) : while (have_posts()) : the_post(); ?> loop here <!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php endwhile; ?> <div id="nav-below" class="navigation"></pre> <div class="nav-previous"><!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'twentyten' ) ); ?></div> <pre></pre> <div class="nav-next"><!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php previous_posts_link( __( 'Newer posts <span class="meta-nav">→</span>', 'twentyten' ) ); ?></div> <pre> </div><!-- #nav-below --> <!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php endif; wp_reset_query(); ?> <!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php get_footer(); ?>