I have the following function that sets up a slideshow in Wordpress. It sets several variables that are dependent on another incremental variable $i. I am trying to find a way to call these variables in my template file so it displays their values. I am envisioning using something like <?php echo $tzDesc; ?>.
The defined variable look like this: $tzDesc = $custom["slide{$i}-desc"]; where it is referring to the value saved in a custom field. Each slide has a custom field of a similar name only differing by the incremental number, the value of $i. I can display them here in this line $page = "<h2>{$tzTitle}</h2><img src='{$tzImage}' />"; and it shows up on my page correctly, however I want more flexibility to call the values in different parts of my template file rather than having to define the entire template file in that one line of the function.
Here is the function:
add_action( 'the_post', 'paginate_slide' );
function paginate_slide( $post ) {
global $pages, $multipage, $numpages;
if( is_single() && get_post_type() == 'post' ) {
$multipage = 1;
$id = get_the_ID();
$custom = array();
$pages = array();
$i = 1;
foreach( get_post_custom_keys() as $key )
if ( false !== strpos( $key, 'slide' ) )
$custom[$key] = get_post_meta( $id, $key, true);
while( isset( $custom["slide{$i}-title"] ) ) {
$page = '';
$tzTitle = $custom["slide{$i}-title"];
$tzImage = $custom["slide{$i}-image"];
$tzDesc = $custom["slide{$i}-desc"];
$tzEmbed = $custom["slide{$i}-embed"];
$page = "<h2>{$tzTitle}</h2><img src='{$tzImage}' />";
$pages[] = $page;
$i++;
}
$numpages = count( $pages );
}
}
I have tried defining the variables outside the function and declaring them in the function as global but that doesn't seem to work.
I have also tried adding one of the variables to the $post object ( by defining $post->tzDesc = $custom["slide{$i}-desc"]; and using <?php $post->ns_tzDesc = $tzDesc; ?> to display the variable. This almost works but it seems to loose the $i variable which defines which slide description to return (eg, slide1-desc or slide2-desc...) and just returns the value for the last slide for every slide.
How can I display the values of these variables and maintain the incremental nature of the function?