0

I'm trying to list the children of a specific parent page for use in a shortcode using get_pages(). Unfortunately, only one page is being displayed though there are many more that should be displayed.

// List Vocations
function get_vocations_list() {

    $args = array (
        'parent' => '753',
        'sort_column' => 'menu_order',
        'sort_order' => 'desc'
    );
    $voc_pages = get_pages($args);

    foreach( $voc_pages as $voc_page ) {
        $voc_link = '<a href="'.get_page_link( $voc_page->ID ).'">'.$voc_page->post_title.'</a>';
        return $voc_link;
    }

}
add_shortcode( 'vocations_list', 'get_vocations_list' );

I've been unsuccessful in troubleshooting this and hope to get some insight from the community as why this is occurring and possible solutions. Thanks in advance.

2

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();
7
  • Thank you for your answer but unfortunately, it is not a working solution. This only returns "Array" instead.
    – CChoma
    Jan 29 '15 at 17:41
  • Will update my answer Jan 29 '15 at 17:45
  • @CChoma You either need to build an array and return that, as suggested by @PieterGoosen, or echo $voc_link. Return "immediately ends the current function's execution".
    – mor7ifer
    Jan 29 '15 at 17:45
  • @m0r7if3r - I did try that and it did not work. echo can't be used in functions used in shortcodes. return must be used. Note that the function works fine when inserted in a template file and return is replaced with echo. But when adding it to functions.php and only changing echo with return, it breaks.
    – CChoma
    Jan 29 '15 at 17:53
  • @CChoma See my update Jan 29 '15 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.