Can anyone spot why my Wordpress loop breaks all the Advanced Custom Fields code that comes after it?

£可爱£侵袭症+ 提交于 2020-01-05 07:29:16

问题


The following loop is running successfully and shows no syntax error in my editor, but it's breaking only the Advanced Custom Fields PHP that comes after it (all the ACF before it works fine, and everything after except ACF works fine).

<?php
    $args=array(
        'post_type' => 'page',
        'post_parent' => '39'
    );

    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>

    <div class="project" style="background-image:url('<?php the_field('preview_thumbnail'); ?>')">
        <div class="project-overlay" style="background-color:<?php the_field('project_highlight_color'); ?>">
        </div>
        <div class="project-content">
            <h3><?php the_title(); ?></h3>
            <p><?php the_field('preview_text'); ?></p>
            <a href="<?php the_permalink(); ?>" class="button arrow-right">Read more</a>
        </div>
    </div>

<?php endwhile; } ?>

Here's an example of what still works after it
<img src="<?php echo get_template_directory_uri(); ?>/images/logo-white.png" />

And here's an example of what's breaking after it (Advanced custom fields code)
<p class="banner-text"><?php the_field('pullout_summary'); ?></p>

Sorry if it's a blatant fix! Thanks in advance.


回答1:


After a custom query, you need to restore the global $post variable of the main query with wp_reset_postdata(). More info in the Codex.



来源:https://stackoverflow.com/questions/27261683/can-anyone-spot-why-my-wordpress-loop-breaks-all-the-advanced-custom-fields-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!