Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Why are two additional <p> elements being created?

So I set up a basic php file for my WordPress theme:

<?php get_header(); ?>
<div class = "content">
  <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <h1><?php the_title(); ?></h1>
    <p><?php the_content(); ?></p>
  <?php endwhile; else : ?>
    <p><?php _e( 'Sorry, no pages found.' ); ?></p>
  <?php endif; ?>
</div>
<?php get_footer(); ?>

When I ran my theme and opened up inspect element, it said that there were two additional <p> elements on top and below my <p> element containing the content.

It looks like this in the inspector:

<h1>Home</h1>
<p></p>
<p>Content</p>
<p></p>

Why is it doing this?

1 Answer

Though I can't give you a why, per se, I can tell you I have that issue with WordPress, Joomla, and pretty much every other platform out there. It is like the CMS wants you to use <p> instead of </br> or margins. (eyeroll). If someone else can give a technical answer, and how this can be avoided, I will also be grateful.

According to some guy on StackOverflow: WordPress replaces new lines with paragraphs in post content.

I, on the other hand, did come up with a temporary fix. Although it wont remove the <p> elements, it will make them invisible in case you want to adjust the padding or margin of your content:

.content p:first-child { /*...or whatever you have your content paragraph in...*/
    margin: 0; 
    padding: 0;
}

.content p:last-child {
    margin: 0;
    padding: 0;
}

If :first-child does not work, use :first-of-type.