I am looking on how to include variable only when needed into a partial which is loaded for every loop.
I have tried set_query_var which works good for including variable but seems like my usage of remove_query_var after doesn’t work properly. What am I missing? Documentation doesn’t say much although it has been added only recently (4.5.0 version)

<ul itemscope itemtype="http://schema.org/ItemList">
    <?php
    // Set variable for partial
    $schema_markup = true; 
    set_query_var('schema_markup', $schema_markup);

    while ($query->have_posts())  :
        $query->the_post(); 

    // Load partial view 
    get_template_part('partials/loop/post-xs');

    endwhile; 
    wp_reset_postdata();

    // Remove variable so that it doesn't trigger when partials are called later in the page where microdata is not needed
    global $wp;
    $wp->remove_query_var('schema_markup');
    ?>
</ul>

Starting code inside partials/loop/post-xs

<li <?php if(!empty($schema_markup)) echo 'itemprop="itemListElement" itemscope itemtype="http://schema.org/Article"'; ?>>

Read more here: How to set variable, pass it to a partial file and remove it after?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: