Your problem is in this section
foreach( $voc_pages as $voc_page ) {
$voc_link = '<a href="'.get_page_link( $voc_page->ID ).'">'.$voc_page->post_title.'</a>';
return $voc_link;
}
You return $voc_link
after just the first iteration, stopping the function and returning its value. You should take return $voc_link;
outside your foreach loop, and probably save your links as an array
Change the above section to
foreach( $voc_pages as $voc_page ) {
$voc_link[] = '<a href="'.get_page_link( $voc_page->ID ).'">'.$voc_page->post_title.'</a>';
}
return $voc_link;
EDIT
From your comment
This only returns “Array” instead
You are most probably trying to echo the function. You cannot echo an array, this will just print the word Array
As the function returns an array, you have two choices
OPTION 1
Put the output through a foreach
loop
$links = get_vocations_list();
foreach ( $links as $link ) {
echo $link;
}
OPTION 2
Convert the array to a string inside the function and then return the string
foreach( $voc_pages as $voc_page ) {
$voc_link[] = '<a href="'.get_page_link( $voc_page->ID ).'">'.$voc_page->post_title.'</a>';
}
$voc_link = implode( '</br>', $voc_link );
return $voc_link;
And then use the function as
echo get_vocations_list();