WordPress get_template_part()打破高级自定义表单转发器

时间:2014-09-06 17:52:54

标签: php html wordpress

我正在WordPress中构建一个页面,该页面使用两个高级自定义表单中继器,并在它们之间的页面中插入模板部分。如果get_template_part()调用在两个转发器之后,所有内容都加载正常,但是当它在两个转发器之间时,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>

我不确定问题是什么。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

发生这种情况是因为在您的模板部分社交中,您使用的是setup_postdata(),它会覆盖当前的$post

因此您必须将$post重置为原始版本。为此,在endforeach之后,您应该致电wp_reset_postdata()。有关更多信息和示例用法,请参阅:http://codex.wordpress.org/Function_Reference/setup_postdata