我有一个称为“案例研究”的自定义分类法,其中填充有虚假帖子。
每个案例研究显然都有不同的内容。为了从自定义字段中提取此内容,我运行了以下代码:
if ( $the_query->have_posts() ) {
while ($the_query->have_posts() ) { // run a loop to get data from all the posts that exist
$the_query->the_post();
tp_get_part( 'templates/snippets/case-study-card',
array(
'heading' => get_the_title(),
'subheading' => get_field( 'case_study_subheading'),
'background' => get_field( 'case_study_background'),
'video' => get_field( 'embedded_video_url'),
)
);
}
echo "</div>";
wp_reset_postdata(); /* Restore original Post Data */
} else {
// no posts found
}
case-study-card.php 具有每个案例研究的HTML。
但是,我还为页面(首页)提供了一个自定义表单字段。
我想从该自定义字段列表中提取数据,但是不确定如何执行。
例如,在上述案例研究场景中,由于存在(并将有很多)案例研究,因此查询是通过while loop
进行的。
但是,对于首页(首页)来说,只会有一个首页,因此不需要循环吗?
希望这有意义吗?
这是我到目前为止的内容:
if ($the_query->have_posts() ) { // check, does the query return any posts?
while ($the_query->have_posts() ) {
$the_query->the_post();
tp_get_part( 'templates/snippets/section-two-overview.php',
array(
'overviewHeading' => get_the_title(),
'overvewSubheading' => get_field( 'overview_subheading'),
'overviewText' => get_field( 'overview_text'),
'overviewBackground' => get_field( 'background_color'),
)
);
}
wp_reset_postdata(); /* Restore original Post Data */
}
section-two-overview.php
<div class="overview-container" style="background-color: <?php echo $overviewBackground; ?>">
<div class="overview-wrapper">
<p>test</p>
<h3><?php echo $overviewHeading; ?></h3>
<h1><?php echo $overvewSubheading; ?></h1>
<p><?php echo $overviewText; ?></p>
</div>
</div>
当前,什么都没有显示,我认为是因为while循环?
P.s。我也尝试了没有循环的情况,即
tp_get_part( 'templates/snippets/section-two-overview.php',
array(
'overviewHeading' => get_the_title(),
'overvewSubheading' => get_field( 'overview_subheading'),
'overviewText' => get_field( 'overview_text'),
'overviewBackground' => get_field( 'background_color'),
)
);
仍然没有任何显示吗?但是如果我echo "success";
会显示?