2017-09-27 42 views
0

我無法訪問我的模板文件中的變量(請參閱下面的註釋)。 提前感謝您的幫助。如何在WordPress中將functions.php中的變量傳遞給模板文件?

的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; ?> 
+1

顯示了functions.php的完整代碼,您使用的是函數.php的代碼。 –

回答

0

讓我們假設你正在使用的get_template_part()功能,包括在function.php文件模板。

現在

通過一定的價值有了這個,你可以使用上面的get_template_part()

functions.php代碼

$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 ); 

set_query_var()功能在template.php文件中,我們需要訪問$mypagemymax直接變量

echo $mypage; 
echo $mymax; 
0
  1. 您需要首先聲明的全局變量中的functions.php的任何功能外,你這樣做不全球關鍵字。

  2. 當你想參考全局變量當其超出範圍(例如,在一個函數,在另一個文件),這是當你使用的global關鍵字。

的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; 
} 

現在,你應該能夠訪問它的template.php,這取決於你使用的是什麼掛鉤(只要該鉤不叫太早

注:

使用全局變量的強烈反對,因此合作考慮你爲什麼要這樣做...你的代碼的邏輯可以改變,還是使用自定義函數?

另一種選擇是使用get_query_var()set_query_var() - 雖然這些是用於傳遞參數而不是查詢字符串。

功能。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 

同樣,這些靠裏的動作序列中正在使用的鉤子。

更新

如果分頁變量是在包括template_part,在這種情況下,你會使用include(locate_template('your-template-part.php'));設置它們之後將它們傳遞給該文件的功能。

+0

變量不會在模板文件中更新,它只是說0 – jordan

+0

@jordan您是否可以使用您現在使用的代碼更新您的問題,包括您嘗試爲$ mypage和$ mymax設置值的操作和功能 – FluffyKitten

+0

我更新了我的代碼! – jordan

相關問題