【问题标题】:Can anyone spot why my Wordpress loop breaks all the Advanced Custom Fields code that comes after it?谁能发现为什么我的 Wordpress 循环会破坏它之后的所有高级自定义字段代码?
【发布时间】:2023-04-02 01:31:02
【问题描述】:

以下循环运行成功,并且在我的编辑器中没有显示语法错误,但它只破坏了它之后的高级自定义字段 PHP(之前的所有 ACF 都可以正常工作,除了 ACF 之后的所有内容都可以正常工作)。

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

这是一个在它之后仍然有效的示例
&lt;img src="&lt;?php echo get_template_directory_uri(); ?&gt;/images/logo-white.png" /&gt;

下面是一个例子,说明它之后的问题(高级自定义字段代码)
&lt;p class="banner-text"&gt;&lt;?php the_field('pullout_summary'); ?&gt;&lt;/p&gt;

抱歉,如果这是一个公然的修复!提前致谢。

【问题讨论】:

  • 没什么明显的,但你所说的休息是什么意思?我假设它的 html 错误?如果是,您应该检查 html 输出以查看正在输出的内容。
  • 好问题。它什么也不显示。因此,在我帖子最底部的代码行(pullout_summary)中,p 标签中没有显示任何内容。这就像 ACF 到达循环的结尾并停止尝试页面的其余部分。

标签: wordpress while-loop syntax-error advanced-custom-fields


【解决方案1】:

自定义查询后,需要用wp_reset_postdata()恢复主查询的全局$post变量。更多信息在Codex

【讨论】:

  • 完美!工作了一个款待。非常感谢。
  • 天哪,我想弄清楚这一点正在失去理智。谢谢你。拯救了我的战利品。
猜你喜欢
  • 1970-01-01
  • 2016-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-12
  • 1970-01-01
  • 1970-01-01
  • 2012-08-10
相关资源
最近更新 更多