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

I have the following function set up in my functions.php file to allow my custom post type “Slideshow” to work.

add_action( 'the_post', 'paginate_slide' );

function paginate_slide( $post ) {

    global $pages, $multipage, $numpages;

    if( is_single() && get_post_type() == 'lom_slideshow' ) {
    $prefix = "slide{$i}";
    $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"] ) ) {

    $hide = $custom["slide{$i}-hide"];
    $slidetype = $custom["slide{$i}-slidetype"];

    if( 'on' == $hide ) {
    $i++;
    continue;
                        }

        $page="";
        $title = $custom["slide{$i}-title"];
        $image = $custom["slide{$i}-image"];
        $desc = $custom["slide{$i}-desc"];
        $embed = $custom["slide{$i}-embed"];
        if(isset($image))
        $imgdisplay = "<img src="https://wordpress.stackexchange.com/questions/4944/{$image}" />";
        else
        $imgdisplay = '';

    if( 'Video' == $slidetype ) 

        $page = "<div class="media-title"><h2>{$title}</h2></div><div class="media-image">{$embed}</div><div class="media-desc"></div>";
    else
        $page = "<div class="media-title"><h2>{$title}</h2></div><div class="media-image">{$imgdisplay}</div><div class="media-desc">{$desc}</div>";
        $pages[] = $page;
        $i++;
    }
    $numpages = count( $pages );
    }
}

This part displays the output:

$page = "<div class="media-title"><h2>{$title}</h2></div><div class="media-image">{$embed}</div><div class="media-desc"></div>";

It displays there where the_content is called in my template. Rather than putting the pieces together there in my functions.php I’d like to be able to call each piece directly in my Custom Post template exactly where I want it. For example, by putting <?php echo $desc; ?> in my template file where I want the Description to display and <?php echo $title; ?> where I want the Title to display.

I’ve tried a couple different ways but can’t get anything to display.

Without starting over from scratch or redesigning my whole template how can I make these variable more modular (able to be placed at will within my template)?

1 Answer
1

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.

Leave a Comment