Pass variables on index to template pages

How do I pass along variables on the index.php template to the other templates in my theme? I’m trying to find a way to pass $ctrl_daily and $ctrl_posts to the template files without it showing up in the main loop. I was able to get it working using set_query_vars but it adds another query to the database and I’m trying to limit queries to as little as possible (plus the ID of the post has been saved to the database).

My code on index.php:

<?php if ( have_posts() ):$alerts = false;$ctrl_daily = false;$articles = false;while( have_posts() ):the_post();?>
<?php if (has_post_format(‘status’)):?>
<?php $alerts = true; ?>
<?php $alerts_posts[] = get_the_ID(); ?>
<?php endif;?>
<?php if (in_category(‘podcast-control-daily’)):?>
<?php $ctrl_daily = true; ?>
<?php $ctrl_posts[] = get_the_ID(); ?>
<?php endif;?>
<?php if (!in_category(‘podcast-control-daily’) && !has_post_format(‘status’)):?>
<?php $articles = true; ?>
<?php $opby_posts[] = get_the_ID(); ?>
<?php endif;?>
<?php endwhile; else : ?>
<?php endif; ?>

<?php $i=1;?>
<?php foreach ($opby_posts as $post):setup_postdata($post);?>
<?php if($i==1):?>
<?php get_template_part( ‘front/first’, get_post_format()); ?>
<?php else:?>
<?php get_template_part( ‘front/bottom’, get_post_format() ); ?>
<?php endif;?>
<?php $i++;?>

Read more here:: Pass variables on index to template pages

Leave a Reply

Your email address will not be published. Required fields are marked *