Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • You had me curious so I played around with this a bit. Thought I would share what I ended up with. It’s a mix of the above two codes modified to fix excerpts breaking on the homepage.

    function insert_disclosure($content) {
    	if( is_singular() && is_main_query()) {
    
    		$paragraph = explode ('</p>', $content); // separate post content into paragraphs
    		$firstp = $paragraph[0]; // select first paragraph
    		preg_match('/(<img.*?>)/', $firstp, $image); // select image tag in first paragraph
    
    		$custom = '
                       <div>This is my disclosure</div>
                   ';
    		$content = $custom . $content;
    
    		$remainder = preg_split('/(<img.*?>)/', $firstp); // select first paragraph content after image
    
    		if ($image) { // if image exists in first paragraph
    		echo $image[0]; // print image
    		echo $custom; // print disclosure
    		echo '<p>'; // open new paragraph
    		echo $remainder[1]; // print remaining first paragraph content
    		}
    
    		else { // if image does not exist in first paragraph
    			echo $custom; // print disclosure
    			echo $firstp; // print first paragraph
    		}
    
    		$therest = implode('', array_slice($paragraph, 1)); // select remaining post content
    		echo $therest; // print remaining post content
    	}
    
    if (is_home() || is_front_page()) {
    	return $content;
    }
    
    }
    add_filter('the_content', 'insert_disclosure');

    Remember to add your first “if” construct back, I had to get rid of it to test because I didn’t have any tagged posts.

    Not sure why return $content; isn’t working there, it works fine elsewhere in the script. I did not have a chance to play with it.

    Hope that helps a bit, especially if you’re using excerpts on the home page.

    Thread Starter walvinmedia

    (@walvinmedia)

    Thanks for the info. I look forward to the update! Keep up the good work! ??

Viewing 2 replies - 1 through 2 (of 2 total)