Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Display the Featured Image after the first paragraph in WordPress
<?php get_header(); ?>
<div>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php
$after_p_img = 1;
$content = apply_filters('the_content', $post->post_content);
if(substr_count($content, '<p>') > $after_p_img){
$contents = explode("</p>", $content);
$p_count = 1;
foreach($contents as $content){
echo $content;
if($p_count == $after_p_img){
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
}
echo "</p>";
$p_count++;
}
}
?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
</div>
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.