Title: Echo inside a while loop
Last modified: August 19, 2016

---

# Echo inside a while loop

 *  Resolved [brian914](https://wordpress.org/support/users/brian914/)
 * (@brian914)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/echo-inside-a-while-loop/)
 * I am trying to do the following to get links to my pages. I am using this:
 *     ```
       if ( have_posts() ) : while ( have_posts() ) : the_post();
       	echo '<li><a href="#">' . the_title() . '</a></li>';
       endwhile; else:
       	//echo 'Sorry, no pages matched your criteria.';
       endif;
       ```
   
 * and this output this in my html:
 * `Client Name<li><a href="#"></a></li>`
 * yet, I want it to output this, like a proper link/li would look like:
 * `<li><a href="#">Client Name</a></li>`
 * Why is that happening and how can I fix this?
 * Thanks a lot for any help with this!

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

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/echo-inside-a-while-loop/#post-2044818)
 * [http://codex.wordpress.org/Function_Reference/the_title](http://codex.wordpress.org/Function_Reference/the_title)
 * two possibilities:
 * either:
 *     ```
       echo '<li><a href="#">'; the_title(); echo '</a></li>';
       ```
   
 * or:
 *     ```
       echo '<li><a href="#">' . the_title('','',false) . '</a></li>';
       ```
   
 *  [Chip Bennett](https://wordpress.org/support/users/chipbennett/)
 * (@chipbennett)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/echo-inside-a-while-loop/#post-2044950)
 * The `the_title()` function echoes by default. Re-echoing it may cause unintended
   output.
 * I would suggest using the following, rather than echoing lines of HTML:
 *     ```
       if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
       	<li><a href="#"><?php the_title(); ?></a></li>
       <?php endwhile; else:
       	//echo 'Sorry, no pages matched your criteria.';
       endif;
       ```
   
 * I assume you’re adding the containing list tags (UL or OL) outside of the Loop?
   Note that if you’re doing so, you’ll want to output your `else` content (e.g.
   _Sorry, no pages matched your criteria._) inside of list-item tags (LI), too,
   in order to maintain correct markup.
 *  Thread Starter [brian914](https://wordpress.org/support/users/brian914/)
 * (@brian914)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/echo-inside-a-while-loop/#post-2045092)
 * Thank you very much for the response!!! This answered my question.

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

The topic ‘Echo inside a while loop’ is closed to new replies.

## Tags

 * [concatenation](https://wordpress.org/support/topic-tag/concatenation/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [while loop](https://wordpress.org/support/topic-tag/while-loop/)

 * 3 replies
 * 3 participants
 * Last reply from: [brian914](https://wordpress.org/support/users/brian914/)
 * Last activity: [15 years, 1 month ago](https://wordpress.org/support/topic/echo-inside-a-while-loop/#post-2045092)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
