Title: Adding a Permalink in a foreach loop
Last modified: August 21, 2016

---

# Adding a Permalink in a foreach loop

 *  [haizdesign](https://wordpress.org/support/users/haizdesign/)
 * (@haizdesign)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/adding-a-permalink-in-a-foreach-loop/)
 * I’m trying to output a list of pages that use a particular page template, with
   their title as a link.
 * This is my code so far:
 *     ```
       <?php
       	$studentpages = get_pages(array(
       	'meta_key' => '_wp_page_template',
       	'meta_value' => 'students.php'
       	));
       	foreach($studentpages as $studentpage){
       	$students = $studentpage->post_title;
       	echo '(<a href="'get_permalink();'">'$students'</a>)'.'<br />';
       	}
       	?>
       ```
   
 * If I just echo $students I get my list but I would like them to be links. This
   is being used on a sitemap and the code is outside of the loop.
 * Can anyone please help?

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

 *  Thread Starter [haizdesign](https://wordpress.org/support/users/haizdesign/)
 * (@haizdesign)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/adding-a-permalink-in-a-foreach-loop/#post-4665509)
 * OK – figured it out:
 *     ```
       <?php
       	$studentpages = get_pages(array(
       	'meta_key' => '_wp_page_template',
       	'meta_value' => 'students.php'
       	));
       	foreach($studentpages as $studentpage){
       	$studentlinks = get_page_link($studentpage->ID);
       	echo '<a href=" '.$studentlinks.' ">'.$studentpage->post_title.'</a>' . '<br />';
       	}
       ?>
       ```
   
 *  Thread Starter [haizdesign](https://wordpress.org/support/users/haizdesign/)
 * (@haizdesign)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/adding-a-permalink-in-a-foreach-loop/#post-4665600)
 * The only thing I can’t use this for is pages using the wordpress page.php, because
   it has no template info.
 * Does anyone know how you access the default template using this method?

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

The topic ‘Adding a Permalink in a foreach loop’ is closed to new replies.

## Tags

 * [get_permalink](https://wordpress.org/support/topic-tag/get_permalink/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 1 participant
 * Last reply from: [haizdesign](https://wordpress.org/support/users/haizdesign/)
 * Last activity: [12 years, 3 months ago](https://wordpress.org/support/topic/adding-a-permalink-in-a-foreach-loop/#post-4665600)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
