Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How do I add $tzDesc and $tzEmbed (and other variables) to the $post object in the following function so that I can display the values in my theme files by inserting <?php echo $tzDesc; ?>?

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 );
    }
}

If you know and can provide an answer can you please be very detailed in how the code should be structured because my knowledge of php is very minimal and I have tried to do this numerous already without any success. Thanks.

share|improve this question
add comment

1 Answer

Like any other php object, you can add items to the $post object like so:

$post->my_new_val_name = 'my new value';

I don't know exactly what you're trying to do, but inside a function hooked to the_post, you can assign new values and return the object.

function my_func($post) {

    $post->my_new_val_name = 'my new value';
    return $post;

}
add_action( 'the_post', 'my_func' );

However, in your template file, you won't be able to just echo $my_new_val_name as you're suggesting... the the_post() function doesn't extract values that way. You'll have to reference the post object explicitly. Like:

echo $post->my_new_val_name;
share|improve this answer
    
I tried adding $post->$tzEmbed = $custom["slide{$i}-embed"]; to the function above and I got an error: Fatal error: Cannot access empty property in... –  matt Dec 7 '10 at 19:09
    
My bad--had a typo in my answer. Leave the $ off of tzEmbed. Do $post->tzEmbed = $your_var; –  MathSmath Dec 7 '10 at 19:33
    
That kind of worked. I put $post->tzEmbed = $custom["slide{$i}-embed"]; in my functions.php file then <?php echo $post->tzEmbed; ?> in my theme file, however it doesn't echo the correct information. The info is stored in the custom fields and the $i variable designates which one. So it should call from the custom field slide1-embed and slide2-embed... Instead its now displaying the contents of the very last slide (slide4-embed) for every slide. Is there any way to preserve the incremental nature of the $i variable? –  matt Dec 7 '10 at 20:26
    
Again, I'm really not sure exactly what you're doing here, but I guess you could store an array in $post->tzEmbed instead of a single value. Build an array containing each of your slide items, then add the array to the post object. $post->tzEmbed = array('myval1', 'myval2'); Then loop through it on the frontend. In any case, I think I've answered your original question, no? –  MathSmath Dec 7 '10 at 21:22
    
I really do appreciate all your help, but no, I don't believe you have answered my original question: How do I add $tzDesc and $tzEmbed (and other variables) to the $post object in the following function so that I can display the values in my theme files by inserting <?php echo $tzDesc; ?>? because with your answer it doesn't correctly display the values for $tzDesc and $tzEmbed. If you look at my function $tzDesc is defined as $tzDesc = $custom["slide{$i}-desc"]; The $custom variable is getting passed along but for some reason it's losing the $i. –  matt Dec 7 '10 at 23:11
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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