WordPress WordPress Theme Development Adding a Blog to a WordPress Theme Coding the Blog Homepage

John Ewell
John Ewell
1,730 Points

Blog post titles not displaying

I am following the video, setting up my home.php file. Sri far everything works, EXCEPT that post titles are not showing up. The code that should display the titles looks like this:

      <article class="post">
            <h1><a href="<?php the_permalink(); ?>"<?php the_title(); ?></a></h1>
        <h2><?php echo strip_tags( get_the_excerpt() ); ?></h2>

On the blog page the excerpt displays with no problem, but the line above it is blank.

When I inspect the generated source code, the h1 tag that should display the title looks like this:

        <h1>
            <a href="http://localhost/localwp.com/2016/11/26/hello-universe/" hello="" universe!<="" a=""></a>
        </h1>                

But it is immediately followed by another version of the title link, which is not embedded in an h1 tag and includes the whole h2 excerpt:

        <a href="http://localhost/localwp.com/2016/11/26/hello-universe/" hello="" universe!<="" a="">
            <h2>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada […]</h2>
        </a>

Is there a bug, or am I doing something wrong?

1 Answer

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 88,477 Points

I suspect the answer is you need to close the opening part of the hyperlink. PHP is just getting confused about the structure of your link tag. :-)

<article class="post">
            <h1>
               <a href=" <?php the_permalink(); ?> "> 
               <?php the_title(); ?></a></h1>
              <h2><?php echo strip_tags( get_the_excerpt() ); ?></h2>

Try putting in a ">" just after your second quotation mark that is the href attribute of your link tag.

John Ewell
John Ewell
1,730 Points

Thanks Jonathan! That was exactly what the problem was. Once I added the angle bracket to close the opening part of the hyperlink tag the titles all magically appeared.

I'm using Linux-based emacs on a mac, which doesn't color code for syntax. Perhaps I should switch editors...