WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 ) {

        $page = '';
        $title = $custom["slide{$i}-title"];
        $image = $custom["slide{$i}-image"];
        $desc = $custom["slide{$i}-desc"];
        $embed = $custom["slide{$i}-embed"];
        $imgdisplay = "<img src='{$image}' />";
        $imgdisplay = '';

    if( 'Video' == $slidetype ) 

        $page = "<div class='media-title'><h2>{$title}</h2></div><div class='media-image'>{$embed}</div><div class='media-desc'></div>";
        $page = "<div class='media-title'><h2>{$title}</h2></div><div class='media-image'>{$imgdisplay}</div><div class='media-desc'>{$desc}</div>";
        $pages[] = $page;
    $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)?

share|improve this question
I'd suggest having a little read on variable scope, see here: php.net/manual/en/language.variables.scope.php – t31os Dec 6 '10 at 22:06
Thanks. So I read through that and I thought it made sense but then I edited my code to define all the variables outside the function, then declare them as global inside the function but it still didn't work. I'm not sure what I'm doing wrong. (fyi, for that exercise I did make the variables a little more unique to avoid conflicts). – matt Dec 6 '10 at 22:48

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.

share|improve this answer
That didn't seem to do anything. – matt Dec 6 '10 at 20:44
Which one? Adding the global declaration, or the wrapping function? – mtekk Dec 6 '10 at 20:46
The wrapping function didin't work. I didn't even see the example for the global, let me try that. – matt Dec 6 '10 at 20:52
Just a note, the 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. – mtekk Dec 6 '10 at 20:52
Yes, the global code I provided should work by just copying it in and pasting it. – mtekk Dec 6 '10 at 20:52

Your Answer


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.