Advertisement
meetsos

Get WP Post Content Without the First Image

Nov 7th, 2015 (edited)
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.26 KB | None | 0 0
  1. <?php
  2. // THIS ONE IN THE THEME'S FUNCTIONS
  3.  
  4. function remove_first_image($the_string) {
  5.     $count = substr_count($the_string, '<img');
  6.     if ($count > 0) {
  7.         $length = strlen($the_string);
  8.         $imgBeg = strpos($the_string, '<img');
  9.         $imgEnd = strpos($the_string, '>', $imgBeg);
  10.         $chop = $length-$imgEnd;
  11.         $new_content = substr($the_string, 0, $imgBeg) . substr($the_string, $imgEnd+1, $chop);
  12.         $capcount = substr_count($new_content, '&#91;caption');
  13.         if ($capcount > 0) {
  14.             $length = strlen($new_content);
  15.             $capBeg = strpos($new_content, '&#91;caption');
  16.             $capClose = strpos($new_content, '/caption&#93;');
  17.             $capEnd = strpos($new_content, '&#93;', $capClose);
  18.             $chop = $length-$capEnd;
  19.             $final_content = substr($new_content, 0, $capBeg) . substr($new_content, $capEnd+1, $chop);
  20.             return $final_content;
  21.         }
  22.         else {
  23.             return $new_content;
  24.         }
  25.     }
  26.     else {
  27.         return $the_string;
  28.     }
  29. }
  30. ?>
  31.  
  32. <?php
  33.  // CALLING THE FUNCTION FROM THE LOOP
  34. ?>
  35.  
  36. <?php while ( have_posts() ) : the_post(); ?>
  37.     <?php $old_content = get_the_content(); ?>
  38.     <?php $new_content = remove_first_image($old_content); ?>
  39.     <?php $display_content = apply_filters('the_content',$new_content); ?>
  40.     <div class="your-content-class">
  41.         <?php echo $display_content; ?>
  42.     </div>
  43. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement