运行 循环后 ACF 未显示
ACF not showing after running loops
在下面的代码中,我从 Advanced Custom Fields 插件调用字段,只有前两个显示 'home_title' 和 'home_content'。在这两个之后,我 运行 两个不同的循环来显示给定类别中的最新帖子。在这些循环 运行 之后,还有 4 个来自 ACF 的字段被调用。 'donate_title'、'donate_content'、'mission_title'、'mission_content'。哪些没有显示(根本没有提取任何内容)。
如果我在 运行 循环之前移动这些 ACF,它们都会正确显示。所以我想这些在循环之后有问题但找不到原因。
<div class="main-site">
<div class="home-title-1">
<?php the_field('home_title'); ?>
</div>
<div class="home-content-1">
<?php the_field('home_content'); ?>
</div>
<div class="home-boxes-cont">
<div class="box-left">
<?php
query_posts('cat=4&posts_per_page=1');
while (have_posts()) : the_post(); ?>
<div class="bl-img">
</div>
<div class="bl-title">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</div>
<div class="bl-content">
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
</div>
<div class="box-middle">
<?php
query_posts('cat=5&posts_per_page=1');
while (have_posts()) : the_post(); ?>
<div class="bm-img">
</div>
<div class="bm-title">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</div>
<div class="bm-content">
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
</div>
<div class="box-right">
<div class="br-img">
</div>
<div class="br-title">
<?php the_field('donate_title'); ?>
</div>
<div class="br-content">
<?php the_field('donate_content'); ?>
</div>
</div>
</div>
<div class="mission-title">
<?php the_field('mission_title'); ?>
</div>
<div class="mission-content">
<?php the_field("mission_content"); ?>
</div>
您正在更改全局 wp_query 变量。当您这样做并且结果不是 is_single()
时,您将无法再提取任何 ACF 设置。
要么将 wp_query 重置为页面的原始设置,要么在创建任何 [=21= 之前将变量存储在数组中] 更改,并在稍后的代码中根据需要检索它们。
虽然我同意 Scriptonomy,但您真的应该使用 get_posts()。这正是此功能旨在执行的操作...主循环之外的自定义循环。您应该很少需要修改全局 wp_query 变量。
如果您仍想使用 the_permalink()
和 the_title()
而不传递 post id,则向下滚动页面至标有 "Access all post data" 的部分,您将查看如何使用 setup_postdata()
使其更容易。
为了在使用 query_posts() 调用更改全局 post 数据后从原始 post 获取自定义字段数据,您需要重置 post 数据与 wp_reset_query() 函数。将此函数放在每个循环之后 -
<?php while (have_posts()) : the_post(); ?>
...
<?php endwhile; wp_reset_query(); ?>
在下面的代码中,我从 Advanced Custom Fields 插件调用字段,只有前两个显示 'home_title' 和 'home_content'。在这两个之后,我 运行 两个不同的循环来显示给定类别中的最新帖子。在这些循环 运行 之后,还有 4 个来自 ACF 的字段被调用。 'donate_title'、'donate_content'、'mission_title'、'mission_content'。哪些没有显示(根本没有提取任何内容)。
如果我在 运行 循环之前移动这些 ACF,它们都会正确显示。所以我想这些在循环之后有问题但找不到原因。
<div class="main-site">
<div class="home-title-1">
<?php the_field('home_title'); ?>
</div>
<div class="home-content-1">
<?php the_field('home_content'); ?>
</div>
<div class="home-boxes-cont">
<div class="box-left">
<?php
query_posts('cat=4&posts_per_page=1');
while (have_posts()) : the_post(); ?>
<div class="bl-img">
</div>
<div class="bl-title">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</div>
<div class="bl-content">
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
</div>
<div class="box-middle">
<?php
query_posts('cat=5&posts_per_page=1');
while (have_posts()) : the_post(); ?>
<div class="bm-img">
</div>
<div class="bm-title">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</div>
<div class="bm-content">
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
</div>
<div class="box-right">
<div class="br-img">
</div>
<div class="br-title">
<?php the_field('donate_title'); ?>
</div>
<div class="br-content">
<?php the_field('donate_content'); ?>
</div>
</div>
</div>
<div class="mission-title">
<?php the_field('mission_title'); ?>
</div>
<div class="mission-content">
<?php the_field("mission_content"); ?>
</div>
您正在更改全局 wp_query 变量。当您这样做并且结果不是 is_single()
时,您将无法再提取任何 ACF 设置。
要么将 wp_query 重置为页面的原始设置,要么在创建任何 [=21= 之前将变量存储在数组中] 更改,并在稍后的代码中根据需要检索它们。
虽然我同意 Scriptonomy,但您真的应该使用 get_posts()。这正是此功能旨在执行的操作...主循环之外的自定义循环。您应该很少需要修改全局 wp_query 变量。
如果您仍想使用 the_permalink()
和 the_title()
而不传递 post id,则向下滚动页面至标有 "Access all post data" 的部分,您将查看如何使用 setup_postdata()
使其更容易。
为了在使用 query_posts() 调用更改全局 post 数据后从原始 post 获取自定义字段数据,您需要重置 post 数据与 wp_reset_query() 函数。将此函数放在每个循环之后 -
<?php while (have_posts()) : the_post(); ?>
...
<?php endwhile; wp_reset_query(); ?>