如何在 WordPress 中重用初始页面内容循环?
How to re-use initial page content loop in WordPress?
我在一个网站上工作,该网站的某个页面有一个简单的模板。
相关页面是关于我们页面。
页面上有第一个主 WordPress 循环,它只是吐出主所见即所得字段的标准内容。
在模板的下面有一些代码吐出一个名为 Staff 的自定义 post 类型的循环。这种自定义 post 类型包含每个员工的信息,并且每个人都在那里吐出来。
我通过后端添加到页面的是使用 ACF Pro 的高级自定义字段。该字段称为 Our Story (our_story) 我希望能够在下面的模板中吐出该字段所有工作人员。
如果我放置以下代码:
<p><?php the_field('our_story') ?></p>
进入位于模板开头的主 WordPress 循环中的模板,然后它显示正常,但我实际上想再次启动相同的循环,但在已经吐出工作人员的循环之下。
如果我将模板开头的完全相同的循环复制到模板的结尾,那么我不会在该代码所在的页面上得到任何输出。因此,我是否需要重置代码或其他东西,或者是否有一种简单的方法可以缓存字段内容并将其放置在模板的其他位置?
抱歉,此处显示了整个模板代码:
<?php
/*
Template Name: About (Staff Listing)
*/
?>
<?php get_header(); ?>
<div class="page-wrap_content">
<div class="center">
<div class="row">
<section class="span8">
<h1><?php the_title(); ?></h1>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'Yes'
)
)
);
$wp_query = new WP_Query( $args );
?>
<?php if (have_posts()) : ?>
<h2>Our Team</h2>
<div>
<?php while (have_posts()) : the_post(); ?>
<article class="service">
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail('service_thumb'); ?></a>
<h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
<!-- <p class="core">Core Staff</p> -->
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo '<a href="/what-we-do/'.$service.'">'.$field['choices'][ $service ].'</a> ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p><a href="<?php the_permalink() ?>" class="boldlink">Read more »</a></p>
</article>
<?php endwhile; ?>
</div>
<?php else : ?>
<p>No core staff found</p>
<?php endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'No'
)
)
);
$wp_query = new WP_Query( $args );
?>
<?php if (have_posts()) : ?>
<h2>Experts</h2>
<div>
<?php while (have_posts()) : the_post(); ?>
<article class="service">
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail('service_thumb'); ?></a>
<h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo '<a href="/what-we-do/'.$service.'">'.$field['choices'][ $service ].'</a> ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p><a href="<?php the_permalink() ?>" class="boldlink">Read more »</a></p>
</article>
<?php endwhile; ?>
</div>
<?php else : ?>
<p>No experts found</p>
<?php endif; ?>
<?php the_field('our_story', '710'); ?>
</section>
<?php get_sidebar(); ?>
</div>
</div>
</div>
<?php get_footer(); ?>
这是因为您需要post id 才能获取该字段。默认是当前post的id。
试试这个:<p><?php the_field('our_story', xxx) ?></p>
其中 xxx 是 关于我们.
的 post ID
我已经调整了您的模板以包含命名的 WP_Query()
循环,并在每个自定义循环之后应用 wp_reset_postdata()
。这应该允许最终的 the_field()
调用访问当前的 post ID,这是高级自定义字段为当前 post 获取 post 元数据所需要的.
在幕后,ACF 运行 (int) get_the_ID();
以获取当前 post 的 ID。如果您的自定义查询循环没有重置,这将不起作用。
<?php
/*
Template Name: About (Staff Listing)
*/
?>
<?php get_header(); ?>
<div class="page-wrap_content">
<div class="center">
<div class="row">
<section class="span8">
<h1><?php the_title(); ?></h1>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'Yes'
)
)
);
// Specify the custom query, so you can build a custom loop.
// ---------------------------------------------------------
$staff_query = new WP_Query( $args );
?>
<?php if ($staff_query->have_posts()) : ?>
<h2>Our Team</h2>
<div>
<?php while ($staff_query->have_posts()): ?>
<?php
// Set up the post to allow use of template tags
// -------------------------------------------------
$staff_query->the_post();
?>
<article class="service">
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail('service_thumb'); ?></a>
<h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
<!-- <p class="core">Core Staff</p> -->
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo '<a href="/what-we-do/'.$service.'">'.$field['choices'][ $service ].'</a> ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p><a href="<?php the_permalink() ?>" class="boldlink">Read more »</a></p>
</article>
<?php endwhile; ?>
<?php
// Need to reset postdata!
// -----------------------------------------------------
wp_reset_postdata();
?>
</div>
<?php else : ?>
<p>No core staff found</p>
<?php endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'No'
)
)
);
$expert_query = new WP_Query( $args );
?>
<?php if ($expert_query->have_posts()) : ?>
<h2>Experts</h2>
<div>
<?php while ($expert_query->have_posts()): ?>
<?php
// Set up post
// -------------------------------------------------
$expert_query->the_post();
?>
<article class="service">
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail('service_thumb'); ?></a>
<h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo '<a href="/what-we-do/'.$service.'">'.$field['choices'][ $service ].'</a> ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p><a href="<?php the_permalink() ?>" class="boldlink">Read more »</a></p>
</article>
<?php endwhile; ?>
<?php
// Need to reset postdata!
// -----------------------------------------------------
wp_reset_postdata();
?>
</div>
<?php else : ?>
<p>No experts found</p>
<?php endif; ?>
<?php
// this should work now:
the_field('our_story');
//the_field('our_story', '710');
?>
</section>
<?php get_sidebar(); ?>
</div>
</div>
</div>
<?php get_footer(); ?>
我认为当自定义循环逻辑全部在模板中处理时很容易混淆 - 我更喜欢通过函数或 class 方法管理自定义循环的 post 数据returns 一组 post 数据 - 然后您可以在模板中遍历它。
希望这对您有所帮助。
资源:https://codex.wordpress.org/Class_Reference/WP_Query#Multiple_Loops
我在一个网站上工作,该网站的某个页面有一个简单的模板。
相关页面是关于我们页面。
页面上有第一个主 WordPress 循环,它只是吐出主所见即所得字段的标准内容。
在模板的下面有一些代码吐出一个名为 Staff 的自定义 post 类型的循环。这种自定义 post 类型包含每个员工的信息,并且每个人都在那里吐出来。
我通过后端添加到页面的是使用 ACF Pro 的高级自定义字段。该字段称为 Our Story (our_story) 我希望能够在下面的模板中吐出该字段所有工作人员。
如果我放置以下代码:
<p><?php the_field('our_story') ?></p>
进入位于模板开头的主 WordPress 循环中的模板,然后它显示正常,但我实际上想再次启动相同的循环,但在已经吐出工作人员的循环之下。
如果我将模板开头的完全相同的循环复制到模板的结尾,那么我不会在该代码所在的页面上得到任何输出。因此,我是否需要重置代码或其他东西,或者是否有一种简单的方法可以缓存字段内容并将其放置在模板的其他位置?
抱歉,此处显示了整个模板代码:
<?php
/*
Template Name: About (Staff Listing)
*/
?>
<?php get_header(); ?>
<div class="page-wrap_content">
<div class="center">
<div class="row">
<section class="span8">
<h1><?php the_title(); ?></h1>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'Yes'
)
)
);
$wp_query = new WP_Query( $args );
?>
<?php if (have_posts()) : ?>
<h2>Our Team</h2>
<div>
<?php while (have_posts()) : the_post(); ?>
<article class="service">
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail('service_thumb'); ?></a>
<h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
<!-- <p class="core">Core Staff</p> -->
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo '<a href="/what-we-do/'.$service.'">'.$field['choices'][ $service ].'</a> ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p><a href="<?php the_permalink() ?>" class="boldlink">Read more »</a></p>
</article>
<?php endwhile; ?>
</div>
<?php else : ?>
<p>No core staff found</p>
<?php endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'No'
)
)
);
$wp_query = new WP_Query( $args );
?>
<?php if (have_posts()) : ?>
<h2>Experts</h2>
<div>
<?php while (have_posts()) : the_post(); ?>
<article class="service">
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail('service_thumb'); ?></a>
<h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo '<a href="/what-we-do/'.$service.'">'.$field['choices'][ $service ].'</a> ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p><a href="<?php the_permalink() ?>" class="boldlink">Read more »</a></p>
</article>
<?php endwhile; ?>
</div>
<?php else : ?>
<p>No experts found</p>
<?php endif; ?>
<?php the_field('our_story', '710'); ?>
</section>
<?php get_sidebar(); ?>
</div>
</div>
</div>
<?php get_footer(); ?>
这是因为您需要post id 才能获取该字段。默认是当前post的id。
试试这个:<p><?php the_field('our_story', xxx) ?></p>
其中 xxx 是 关于我们.
我已经调整了您的模板以包含命名的 WP_Query()
循环,并在每个自定义循环之后应用 wp_reset_postdata()
。这应该允许最终的 the_field()
调用访问当前的 post ID,这是高级自定义字段为当前 post 获取 post 元数据所需要的.
在幕后,ACF 运行 (int) get_the_ID();
以获取当前 post 的 ID。如果您的自定义查询循环没有重置,这将不起作用。
<?php
/*
Template Name: About (Staff Listing)
*/
?>
<?php get_header(); ?>
<div class="page-wrap_content">
<div class="center">
<div class="row">
<section class="span8">
<h1><?php the_title(); ?></h1>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'Yes'
)
)
);
// Specify the custom query, so you can build a custom loop.
// ---------------------------------------------------------
$staff_query = new WP_Query( $args );
?>
<?php if ($staff_query->have_posts()) : ?>
<h2>Our Team</h2>
<div>
<?php while ($staff_query->have_posts()): ?>
<?php
// Set up the post to allow use of template tags
// -------------------------------------------------
$staff_query->the_post();
?>
<article class="service">
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail('service_thumb'); ?></a>
<h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
<!-- <p class="core">Core Staff</p> -->
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo '<a href="/what-we-do/'.$service.'">'.$field['choices'][ $service ].'</a> ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p><a href="<?php the_permalink() ?>" class="boldlink">Read more »</a></p>
</article>
<?php endwhile; ?>
<?php
// Need to reset postdata!
// -----------------------------------------------------
wp_reset_postdata();
?>
</div>
<?php else : ?>
<p>No core staff found</p>
<?php endif; ?>
<?php
$args = array(
'post_type' => 'staff',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'staff',
'compare' => '==',
'value' => 'No'
)
)
);
$expert_query = new WP_Query( $args );
?>
<?php if ($expert_query->have_posts()) : ?>
<h2>Experts</h2>
<div>
<?php while ($expert_query->have_posts()): ?>
<?php
// Set up post
// -------------------------------------------------
$expert_query->the_post();
?>
<article class="service">
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail('service_thumb'); ?></a>
<h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
<?php
if( get_field('expert') ) {
$field = get_field_object('expert');
$services = get_field('expert');
echo '<p class="expert">Expert in: ';
foreach($services as $service){
echo '<a href="/what-we-do/'.$service.'">'.$field['choices'][ $service ].'</a> ';
}
echo '</p>';
}
?>
<p><?php the_field('excerpt'); ?></p>
<p><a href="<?php the_permalink() ?>" class="boldlink">Read more »</a></p>
</article>
<?php endwhile; ?>
<?php
// Need to reset postdata!
// -----------------------------------------------------
wp_reset_postdata();
?>
</div>
<?php else : ?>
<p>No experts found</p>
<?php endif; ?>
<?php
// this should work now:
the_field('our_story');
//the_field('our_story', '710');
?>
</section>
<?php get_sidebar(); ?>
</div>
</div>
</div>
<?php get_footer(); ?>
我认为当自定义循环逻辑全部在模板中处理时很容易混淆 - 我更喜欢通过函数或 class 方法管理自定义循环的 post 数据returns 一组 post 数据 - 然后您可以在模板中遍历它。
希望这对您有所帮助。
资源:https://codex.wordpress.org/Class_Reference/WP_Query#Multiple_Loops