• haizdesign

    (@haizdesign)


    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

    (@haizdesign)

    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

    (@haizdesign)

    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.