0

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.

1

Managed to work it out, the problem was down to how I had set up my home page to display only my custom posts.

I had set a static home page in Settings > Reading and I was using the following new WP_Query in front-page.php to show all my CPT 'portfolio' posts.

<?php 
$args = array(
    'post_type' => array('portfolio'),
    'posts_per_page' => '-1'
    );

$the_query = new WP_Query( $args );

    if ( $the_query->have_posts() ) :
        while ( $the_query->have_posts() ) : $the_query->the_post();
            get_template_part( 'content', 'portfolio' );
        endwhile;
    endif; ?>

The Fix:

I changed the Settings > Reading back to their default settings

I changed the query on front-page.php to the standard query:

<?php while ( have_posts() ) : the_post();

get_template_part( 'content', 'portfolio' );

endwhile; ?>

I used the pre_get_posts to modify the main query to only show my custom post type as explained here http://justintadlock.com/archives/2010/02/02/showing-custom-post-types-on-your-home-blog-page

After this my function to localize the variable worked on the front-page.php template.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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