Passing variables to template parts

I have one template file videos.php which has the following line of code in it (aswell as a load of HTML):

<?php get_template_part('loop', 'feed-videos' ); ?>

inside that template part, I have the following:

<?php $video = 'video-' . $post->ID; ?>
<?php get_template_part( 'include', 'modal-video' ); ?>

I would then like to be able to use the $video variable inside include-modal-video.php.

So at the top of include-modal-video.php I have:

<?php global $video; ?>

Further down that file, I have <h2>00: <?php echo $video; ?></h2>

But I get nothing output from that line of code. All I see is the following indicator of where the value should be

00

Can anyone see what Im doing wrong?

2 Answers
2

If you use locate_template() instead of get_template_part() you can use all variables in that script:

include(locate_template('include-modal-video.php'));

Then, <h2>00: <?php echo $video; ?></h2> will work.

Leave a Comment