Title: WHy does this PHP ruin the loop?
Last modified: August 21, 2016

---

# WHy does this PHP ruin the loop?

 *  [CAhlback](https://wordpress.org/support/users/cahlback/)
 * (@cahlback)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/why-does-this-php-ruin-the-loop/)
 * So this code below is for the page template. My goal is to make it possible to
   use the featured image as a headline, if there is a featured image. Otherwise,
   I want it to simply output the title, as usually. However, when I try this code
   out, the result is an entire blank page, and the web debugger shows no code at
   all, so somehow this makes the server send the client/web browser a totally blank
   page.
    Any ideas? /Christoffer
 *     ```
       <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
       <?php if ( has_post_thumbnail()) {
       echo get_the_post_thumbnail( $post_id, $size, $attr ); }
       else {
   
       echo"<h1>the_title()</h1>";
       echo"<div class="hrThickFull"></div>";
       }
       ?>
       ```
   
 * _[Please post code & markup between backticks or use the code button. Your posted
   code may now have been permanently damaged by the forum’s parser.]_

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

 *  [Justin](https://wordpress.org/support/users/jgwpk/)
 * (@jgwpk)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/why-does-this-php-ruin-the-loop/#post-3693022)
 * The function `the_title()` prints out the function already. You do not need to
   echo the this function.
 * You can pass wrapper paramaters to `the_title()` like so:
 *     ```
       <?php the_title('<h1>','</h1>'); ?>
       ```
   
 *  [Justin](https://wordpress.org/support/users/jgwpk/)
 * (@jgwpk)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/why-does-this-php-ruin-the-loop/#post-3693025)
 * Sorry, here is a refference to the the codex
 * [http://codex.wordpress.org/Function_Reference/the_title](http://codex.wordpress.org/Function_Reference/the_title)
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/why-does-this-php-ruin-the-loop/#post-3693029)
 * This line has un-escaped double quotes in it.
 *     ```
       echo"<div class="hrThickFull"></div>";
       ```
   
 * change it to:
 *     ```
       echo'<div class="hrThickFull"></div>';
       ```
   
 * or
 *     ```
       echo"<div class=\"hrThickFull\"></div>";
       ```
   
 *  Thread Starter [CAhlback](https://wordpress.org/support/users/cahlback/)
 * (@cahlback)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/why-does-this-php-ruin-the-loop/#post-3693038)
 *     ```
       <?php if ( has_post_thumbnail()) {
       echo get_the_post_thumbnail( $post_id, $size, $attr ); }
       else {
   
       <?php the_title('<h1>','</h1>'); ?>
       echo'<div class="hrThickFull"></div>';
       }
       ?>
       ```
   
 * still not working… Did I miss anything?
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/why-does-this-php-ruin-the-loop/#post-3693053)
 * Remove the closing php tags (<?php ?>) around the_title():
 *     ```
       <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
       <?php
       	if ( has_post_thumbnail() ) {
       		echo get_the_post_thumbnail( $post_id, $size, $attr );
       	} else {
   
       	the_title('<h1>','</h1>');
       	echo'<div class="hrThickFull"></div>';
       }
       ?>
       ```
   
 * I also assume you close the loop with an endwhile and endif after this code:
 *     ```
       <?php endwhile; ?>
       <?php endif; ?>
       ```
   
 *  Thread Starter [CAhlback](https://wordpress.org/support/users/cahlback/)
 * (@cahlback)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/why-does-this-php-ruin-the-loop/#post-3693060)
 * That does it! Thanks!
 * Yes, the loops has always been closed at the end.
 * But I haven’t really understood the closing php tags (<?php ?>). Sometimes they
   occur within {}-clams but this time they really made a mess.
 * Note, I’m new at this, and I’m learning by the copy-paste-watch-read-and-learn-
   method. 🙂
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/why-does-this-php-ruin-the-loop/#post-3693089)
 * It’s just not allowed to have opening or closing php tags inside a php tag:
 *     ```
       <?php
       if(is_home()) {
       // this will throw an error
       <?php echo 'hello world'; ?>
       }
       ?>
       ```
   
 * What you coud do is this:
 *     ```
       <?php if(is_home()) { ?>
       <?php echo 'hello world'; ?>
       <?php  } ?>
       ```
   
 * As you can see there are no opening or closing php tags inside another php tag.
 * it’s probably better to use an alternative syntax (to improve legibility) in 
   a situation where you have a mix of html and php: [http://php.net/manual/en/control-structures.alternative-syntax.php](http://php.net/manual/en/control-structures.alternative-syntax.php)
 * I’m glad you’ve got it resolved.
 *  Thread Starter [CAhlback](https://wordpress.org/support/users/cahlback/)
 * (@cahlback)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/why-does-this-php-ruin-the-loop/#post-3693100)
 * *Singing “I can see clearly now, the rain is gone..”
    hehe
 * Yeah, I’ve seen those colon syntaxes. I think I’ll try that on this code. 🙂 
   Thanks for helping me out! 🙂
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/why-does-this-php-ruin-the-loop/#post-3693123)
 * No problem 🙂
 * Something to look into regarding php and strings:
    [http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php](http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php)
   [http://php.net/manual/en/language.types.string.php](http://php.net/manual/en/language.types.string.php)
   [http://make.wordpress.org/core/handbook/coding-standards/php/#single-and-double-quotes](http://make.wordpress.org/core/handbook/coding-standards/php/#single-and-double-quotes)
   When you edit your php files try and use an editor with syntax highlighting that
   will show errors when you accidentally don’t escape quotes properly. [http://codex.wordpress.org/Editing_Files#Acceptable_External_Editors](http://codex.wordpress.org/Editing_Files#Acceptable_External_Editors)

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

The topic ‘WHy does this PHP ruin the loop?’ is closed to new replies.

## Tags

 * [featured image](https://wordpress.org/support/topic-tag/featured-image/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [the-loop](https://wordpress.org/support/topic-tag/the-loop/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 9 replies
 * 3 participants
 * Last reply from: [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * Last activity: [13 years, 1 month ago](https://wordpress.org/support/topic/why-does-this-php-ruin-the-loop/#post-3693123)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
