I have the following code in a php include file:
function pass_the_var() {
$video_embed_code = jkaw_oembed_code();
return array(
'embedCode' => $video_embed_code
);
}
earlier in the same file I also enqueue my scripts:
wp_register_script('jkaw-js', get_template_directory_uri() . '/lib/js/jkaw.js', false, '1.0', true);
wp_enqueue_script( 'jkaw-js' );
wp_localize_script( 'jkaw-js', 'phpVars', pass_the_var() );
I am using this code on a custom post type 'portfolio'. wp_localize_script
successfully returns the array and populates the $video_embed_code variable on single-portfolio.php
and on taxonomy-portfolio-cat.php
.
However on front-page.php
template (I am using a static front page with it's own wp_query to list all the 'portfolio' posts), the $video_embed_code is returned as an empty string.
single-portfolio.php
, taxonomy-portfolio-cat.php
and front-page.php
all call
get_template_part( 'content', 'portfolio' );
which contains an if.. else.. statement.
if ( !is_single() ) { ?>
<a class="wrapper" style="width:100%; padding-bottom: <?php echo $thumb['padding']; ?>%" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<div class="inner">
<img class="ll" data-original="<?php echo $thumb['src']; ?>" />
</div>
</a>
<?php } else {
// code for a single post
} ?>
If I call pass_the_var()
inside the if(!is_single)
statement it returns the correct code but for some reason when it's called via the wp_localize_script
on front-page.php
it fails.
I guess this has something to do with the loop and the order that things are fired but I cannot work out why it is failing and how to solve the problem. Any suggestions would be gratefully received, thanks.