I can't access my variables inside my template file (see comment below). thanks in advance for the help.
functions.php:
$mypage = (get_query_var('paged')) ? get_query_var('paged') : 1;
$maxpost = $loop->max_num_pages;
template.php:
$args = array(
'post_type' => 'publications',
'posts_per_page' => 2,
);
$query = new WP_Query( $args ); ?>
<?php if ($query->have_posts()) : ?>
<div id="posts">
<?php while ($query->have_posts()) : $query->the_post(); ?>
<section class="col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2 publications-section tagged-posts post">
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php echo get_permalink( $post->ID ); ?>">
<div class="col-sm-4 col-sm-push-8 publications-section-image" style="background-image:url('<?php the_post_thumbnail_url(); ?>');"></div>
</a>
<?php endif; ?>
<div class="col-sm-8 col-sm-pull-4">
<h2><a href="<?php echo get_permalink( $post->ID ); ?>"><?php the_title(); ?></a></h2>
<p><?php the_excerpt(); ?></p>
<small><?php echo get_post_meta($post->ID, "_contact", true); ?></small>
<small class="pub-tags pub"> <span><?php the_terms( $post->ID,'mytag','',' '); ?></span></small>
</div>
</section>
<?php echo $mypage; endwhile; wp_reset_postdata(); ?>
</div>
<div class="col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2" id="pub-btn-section">
<input type="submit" class="btn" id="loadmore" name="" value="Submit">
</div>
<?php endif; ?>
let's assume that your are used the get_template_part()
function to include template in function.php
file.
now pass some value with this you can used the set_query_var()
function above the get_template_part()
functions.php
code
$mypage = (get_query_var('paged')) ? get_query_var('paged') : 1;
$mymax = $loop->max_num_pages;
set_query_var( 'mypage', absint( $mypage ) );
set_query_var( 'mymax', absint( $mymax) );
get_template_part( 'The slug name for the generic template', $name = null );
In
template.php
file, we need to access the$mypage
andmymax
variable directly
echo $mypage;
echo $mymax;
You need to declare your global variable in functions.php outside of any functions first, and you do this without the global keyword.
When you want to reference the global variable when its out of scope (eg in a function, in another file), that's when you used the global
keyword.
functions.php :
// declare your global variables
$mypage = 0;
$mymax = 0;
function myfunction(){
global $mypage,$mymax; // reference your global variables
$mypage = (get_query_var('paged')) ? get_query_var('paged') : 1;
$mymax = $loop->max_num_pages;
}
Now you should be able to access it in template.php, depending on what hook you are using (as long as that hook isn't called too early.
NOTE:
The use of global variables is strongly discouraged, so consider why you're doing it this way... could the logic of your code be changed, or a custom function be used instead??
Another option is to use get_query_var()
and set_query_var()
- although these are intended for passing parameters instead of on the querystring.
functions.php
$mypage = (get_query_var('paged')) ? get_query_var('paged') : 1;
set_query_var("mypage", $mypage ); // set your variable
template.php
$mypage = get_query_var("mypage"); // get the variable
Again, these depend on where in the sequence of actions the hook you are using is.
Update :
If your paging variables are in a function that includes a template_part , in that case you would use include(locate_template('your-template-part.php'));
after setting them to pass them to that file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.