0

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?

2 Answers 2

1

after the $i++; try the following :

$my_vars[$i]['tzTitle']=$tzTitle;
$my_vars[$i]['tzImage']=$tzImage;
$my_vars[$i]['tzDesc']=$tzDesc;
$my_vars[$i]['tzEmbed']=$tzEmbed;

then before exiting the function just return $my_vars; now you can call the function $x=paginate_slide('YOUR POST');

and use the values as $x[1]['tzTitle']; where you can replace the 1 with value of desired $i

Sign up to request clarification or add additional context in comments.

1 Comment

2 questions: 1) Is there any way to keep the page/slide number dynamic so it is filled in by the function? The function is what automatically paginates the slides. My template file sets up a generic template, not one for each slide. 2) Where should I be putting $x=paginate_slide('YOUR POST');?
0

Why don't you have the function return $i?

2 Comments

When I do I loose the pagination and it only shows one slide.
Ok, I moved where I returned $i to the end of the function before the last { and I have my slides back but it's still showing the same value for every slide.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.