Forums

[resolved] Get all pages in theme's function.php file - only getting first page (3 posts)

  1. pavy
    Member
    Posted 2 years ago #

    I'm trying generate all my page links from my theme's function.php file. Right now what I have works, but it only gets the first page. I think this may be due to the fact it's not in a loop, but I'm not that familiar with doing this in the functions file as I normally generate my page links using wp_list_pages() in my theme file.

    Here is the block of code I'm currently using:

    function sitenav() {
    	$pages = get_pages();
    	foreach ($pages as $pagg) {
    	$option = '<li>';
    	$option .= '<a href="' . get_page_link($pagg->ID) . '">';
    	$option .= $pagg -> post_title;
    	$option .= '</a></li><li>:</li>';
    	return $option;
    	}
    }

    And then, I'm putting this function in a variable string, along with other HTML I need in the header area of my theme. And finally, I'm adding the action into a hook and echoing the full output.

    Any suggestions on what I need to change/fix/add? Thanks!

  2. MichaelH
    Volunteer
    Posted 2 years ago #

    This:

    function sitenav() {
    	$pages = get_pages();
    	foreach ($pages as $pagg) {
    	$option = '<li>';

    should be:

    function sitenav() {
            $option = '';
    	$pages = get_pages();
    	foreach ($pages as $pagg) {
    	$option .= '<li>';
  3. pavy
    Member
    Posted 2 years ago #

    Thanks. I ended up with this:

    function sitenav() {
    	$pages = get_pages();
    	$optionstring = '';
    	foreach ($pages as $pagg) {
    		$option = '<li>';
    		$option .= '<a href="' . get_page_link($pagg->ID) . '">';
    		$option .= $pagg -> post_title;
    		$option .= '</a></li><li>:</li>';
    		$optionstring = $optionstring . $option;
    	}
    	return $optionstring;
    }

Topic Closed

This topic has been closed to new replies.

About this Topic