Take Variables Set in Functions.php and Echo Them Inside My Custom Post Template

What you need to do is look at your $page and $pages variables and do the same thing with your $desc variable (and any other variable that you want to access outside of the function).

You will need a global $descs which will be an array where you will place in each of the $desc values, using something like $descs[] = $desc; right after the code $pages[] = $page;. Then you would access $descs outside of the function, remember it is an array.


Usually, you need to declare $desc as a global in the template file you want to use it in, e.g. you need to use this:

<?php global $desc;
echo $desc;?>

It may be better to use a wrapping function that returns, or echos your $desc variable and call that from within your template file, e.g. place something like this in your functions.php:

function foo_bar()
{
$custom[$key] = get_post_meta( $id, $key, true);
return $custom["slide{$i}-desc"];
}

and then in your template file use this:

<?php echo foo_bar();?>

Note, this wrapping function won’t work as is, since it looks like you grabbed something from within a PHP loop construct. You may need to pass in $id, $key, $i, and $custom to the function, or declare them as globals in the function. Without knowing the context, I can’t tell you which way will work better for you.