2014-09-06 46 views
0

我在WordPress中建立一個頁面,它使用兩個高級自定義形式中繼器,並在他們之間的頁面插入模板部分。如果get_template_part()調用在兩個中繼器之後,所有內容都可以正常加載,但是當它位於兩個中繼器之間時,get_template_part()之後的內容不會加載,我不知道爲什麼。WordPress的get_template_part()打破先進的自定義形式中繼器

<?php get_header(); ?> 
<div class="clearfix" role="main" > 

     <?php if(get_field('landing')):?> 
      <?php while (have_rows('landing')) : the_row(); ?> 
       <section class="block row" id="landing" style="background-image: url('<?php the_sub_field('background'); ?>');"> 
         <div class="text-center"> 

          <img src="<?php the_sub_field('image'); ?>" />        
          <p class=""><?php echo the_sub_field('text'); ?></p> 
          <a class="" href="<?php the_sub_field('section_url');?>"><?php the_sub_field('url_text'); ?></a> 
          <a href="#" class="text-center"><img src="<?php bloginfo('template_directory');?>/images/more_icon.png"></img></a> 
         </div> 
       </section> <!-- end article header --> 
      <?php endwhile; ?> 
     <?php endif; ?> 

    <?php get_template_part ('social');?> 


     <?php if(get_field('section')):?> 
      <?php while (have_rows('section')) : the_row(); ?> 
       <section class="block row" style="background-image: url('<?php the_sub_field('background'); ?>');"> 
         <div class="text-center"> 

          <img src="<?php the_sub_field('image'); ?>" />        

          <p class=""><?php echo the_sub_field('text'); ?></p> 
          <a class="" href="<?php the_sub_field('section_url');?>"><?php the_sub_field('url_text'); ?></a> 
          <a href="#" class="text-center"><img src="<?php bloginfo('template_directory');?>/images/more_icon.png"></img></a> 
         </div> 
       </section> <!-- end article header --> 
      <?php endwhile; ?> 
     <?php endif; ?> 



</div> <!-- end #content --> 

下面是get_template_part()調用的代碼:

<div class="row block"> 
<div class="col-sm-3"> 
    <h3>Instagram</h3> 
    <p>This is an instagram feed</p> 
</div> 
<div class="col-sm-3"> 
    <h3>On The Radio</h3> 
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
     Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, 
     when an unknown printer took a galley of type and scrambled it to make a type 
     specimen book. It has survived not only five centuries, but also the leap</p> 
</div> 
<div class="col-sm-3"> 
    <h3>In The News</h3> 
    <p><?php 
     global $post; 
     $args = array('posts_per_page' => 3); 
     $myposts = get_posts($args); 

     foreach($myposts as $post) : setup_postdata($post); ?> 
     <a href="<?php the_permalink() ?>" class="pull-right" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?><br /> 
     <?php echo get_the_date(); ?></a> 
     <?php the_post_thumbnail(); ?> 
     <?php endforeach; ?></p> 
</div> 
<div class="col-sm-3"> 
    <div class="text-center"><img src="<?php bloginfo('template_directory');?>/images/reclamation_road.png"/></div> 
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem 
     Ipsum has been the industry's standard dummy text ever since the 1500s, when an 
     unknown printer took a galley of type and scrambled it to make a type specimen 
     book. It has survived not only five centuries, but also the leap</p> 
</div> 

我不知道是什麼問題。任何幫助將不勝感激。

回答

2

發生這種情況是因爲在您的模板部分社交中,您使用的是setup_postdata(),它將覆蓋當前的$post

所以你必須重置$post到原來的。要做到這一點,在endforeach之後,您應該撥打wp_reset_postdata()。欲瞭解更多信息和示例用法,你可以參考:http://codex.wordpress.org/Function_Reference/setup_postdata

+0

真棒,工作像一個魅力。 – 2014-09-06 18:23:46

+0

很高興工作。如果它對你有幫助,請隨時接受答案。 – 2014-09-06 18:33:11

+0

WWWWwwwhhhyyyyyy?!?! (謝謝) – CenterOrbit 2015-08-07 23:21:13