Skip to content

Instantly share code, notes, and snippets.

@antcms
Created June 16, 2016 04:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antcms/14555776302dc18a38fbde41278841d0 to your computer and use it in GitHub Desktop.
Save antcms/14555776302dc18a38fbde41278841d0 to your computer and use it in GitHub Desktop.
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