【发布时间】:2020-09-01 13:50:40
【问题描述】:
我正在使用 ACF 插件为我的 wordpress 网站创建自定义字段。当我将代码全部放在同一页面上时,一切顺利,零问题。但是,当我将代码分解为模板部分时,每个部分的英雄和自定义字段的 ACF 停止输出数据。几乎就像在模板部分中使字段读为空一样,但是当我重新排序 get_template_parts php(即,在 content-pizza 之前移动 content-rolls 或反之亦然)时,无论哪个部分首先正确显示,而第二个没有.
<?php get_template_part('template-parts/home/content', 'callout'); ?>
<?php get_template_part('template-parts/home/content', 'menu'); ?>
<?php get_template_part('template-parts/home/content', 'pizza'); ?>
<?php get_template_part('template-parts/home/content', 'rolls'); ?>
如果我将 content-menu.php 移动到 content-pizza.php 下,也会发生同样的事情——ACF 停止显示后端已填充任何内容。我怀疑问题出在 content-pizza.php(content-rolls.php 是复制的相同代码,填充了正确的变量),但我看不出问题出在哪里。
content-pizza.php页面编码为:
<?php
/**
* Template part for displaying pizza content in page-home.php
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/
*
* @package JJs_Pizza
*/
// Variables
$pizza_hero = get_field('pizza_hero');
$pizza_custom = get_field('pizza_custom');
$pizza_args = array(
'posts_per_page' => 50,
'post_type' => 'food',
'category_name' => 'pizza',
'orderby' => 'post_id',
'order' => 'ASC'
);
?>
<!-- Pizza Section -->
<section class="menu-pizza mt-4" id="pizza">
<div class="row no-gutters p-3">
<div class="col">
<?php if( !empty($pizza_hero) ) : ?>
<?php echo($pizza_hero); ?>
<?php endif; ?>
</div>
</div>
<div class="row p-3 justify-content-center">
<?php if ( !empty($pizza_custom) ) : ?>
<div class="col-lg-4 col-sm-6 col-xs-12 custom-menu pt-2">
<?php echo($pizza_custom);?>
</div>
<?php endif; ?>
<?php $loop = new WP_Query($pizza_args);?>
<?php while( $loop ->have_posts() ) : $loop->the_post(); ?>
<div class="col-lg-4 col-sm-6 col-xs-12 menu p-4">
<div class="row">
<div class="col d-flex justify-content-center">
<?php if (get_field('image')):?>
<img src=”<?php the_field('image'); ?>" class="img-fluid">
<?php endif; ?>
</div>
</div>
<div class="row">
<div class="col">
<h3 class="text-center"><?php the_title();?></h3>
<p class="text-center mx-auto">
<?php
$labels = get_field(‘labels’);
if( $labels && in_array(‘vegetarian’, $labels) ) {
echo(‘<i class="fas fa-leaf"></i>');
}
?>
<?php
$labels = get_field(‘labels’);
if( $labels && in_array(‘spicy’, $labels) ) {
echo(‘<i class="fas fa-fire"></i>');
}
?>
</p>
<?php if (get_field('description')):?>
<p class="text-center"><em><?php the_field('description');?></em></p>
<?php endif; ?>
</div>
</div>
</div>
<?php endwhile;?>
</div>
</section><!-- End Pizza Section -->
我还将目前拥有的代码上传到https://demo.jjs-pizza.com/ 的实时站点,但到目前为止,我编写的唯一动态部分是比萨饼和比萨卷部分,因此您可以了解这个问题。
提前感谢您的帮助!如果情况更糟,我会删除模板部分并在同一页面上工作,但为了代码更简洁,我希望有一个解决方案。
【问题讨论】:
标签: php wordpress advanced-custom-fields