Php get_template_part()无法使用高级自定义字段插件
我在WordPress项目中工作,我在一个名为etrisonials.php的特殊文件中创建了推荐,我在页面上通过get_template_part调用了关于我们的文件,它工作正常,但当我在主页上调用(etrisonials.php)时,并没有显示输入的所有部分 使用高级自定义字段插件 此代码Php get_template_part()无法使用高级自定义字段插件,php,wordpress,advanced-custom-fields,Php,Wordpress,Advanced Custom Fields,我在WordPress项目中工作,我在一个名为etrisonials.php的特殊文件中创建了推荐,我在页面上通过get_template_part调用了关于我们的文件,它工作正常,但当我在主页上调用(etrisonials.php)时,并没有显示输入的所有部分 使用高级自定义字段插件 此代码 <!-- Start Section Testimonials --> <section class="testimonials section-padding"> <
<!-- Start Section Testimonials -->
<section class="testimonials section-padding">
<div class="carousel-right col-lg-7 col-md-7 col-sm-7 col-xs-12">
<div class="owl-carousel">
<?php $testimonials = array ('post_type' => 'Testimonials' , 'order' => 'ASC');
$query = new wp_query($testimonials);
if ($query->have_posts()) {
while ($query->have_posts()){
$query->the_post(); ?>
<!-- Start Item 1 -->
<div class="testimonials-item">
<!-- Testimonials Text -->
<div class="testimonials-text-item">
<?php the_content(); ?>
</div>
<!-- Testimonials Title -->
<div class="testimonials-title clearfix">
<!-- Title Img -->
<div class="title-img">
<img src="<?php the_field('image'); ?>" alt="testimonials">
</div>
<!-- Title Text -->
<div class="title-text">
<h3><?php the_title(); ?></h3>
<p><?php the_field('small_title'); ?></p>
</div>
</div>
</div>
<!-- End Item 1 -->
<?php }} ?>
</div>
</div>
<?php wp_reset_postdata(); ?>
<!-- Start Title -->
<?php $testimonials = get_field('testimonials'); ?>
<div class="container">
<div class="row">
<div class="col-lg-4 col-md-5 col-md-5 col-sm-4 col-xs-12">
<div class="testimonials-text clearfix">
<div class="title">
<span><?php echo $testimonials['small_title']; ?></span>
<h2><?php echo $testimonials['main_title']; ?></h2>
</div>
<div class="text-p">
<?php echo $testimonials['description']; ?>
</div>
</div>
</div>
</div>
</div>
<!-- End Title -->
</section>
“alt=”推荐信“>
未从中显示的部分
所有输入
<span> <? php echo $ testimonials ['small_title']; ?> </ span>
<h2> <? php echo $ testimonials ['main_title']; ?> </ h2>
<? php echo $ testimonials ['description']; ?>
1-基于代码的get_字段(“推荐”)需要是一个数组,因此最好的方法是为该函数指定默认值数组,如:
$testimonials = get_field('testimonials', array(
'small_title' => '',
'main_title' => '',
'description' => ''
));
echo $testimonials['small_title'];
echo $testimonials['main_title'];
echo $testimonials['description'];
echo isset($testimonials['small_title']) ? $testimonials['small_title'] : '';
echo isset($testimonials['main_title']) ? $testimonials['main_title'] : '';
echo isset($testimonials['description']) ? $testimonials['description'] : '';
因此,如果字段不是数组或null,则通过这种方式设置默认值,因为有时,如果数据库中没有记录当前post的数据,则此函数将返回null
2-以下字符串通过给定键获取数组$ESTIONAL值:
$testimonials = get_field('testimonials', array(
'small_title' => '',
'main_title' => '',
'description' => ''
));
echo $testimonials['small_title'];
echo $testimonials['main_title'];
echo $testimonials['description'];
echo isset($testimonials['small_title']) ? $testimonials['small_title'] : '';
echo isset($testimonials['main_title']) ? $testimonials['main_title'] : '';
echo isset($testimonials['description']) ? $testimonials['description'] : '';
但是,如果数组$commentials中不存在密钥,该怎么办?
您需要将PHP函数与速记条件if语句一起使用,以避免出现警告消息或破坏html代码
这是正确的方法:
$testimonials = get_field('testimonials', array(
'small_title' => '',
'main_title' => '',
'description' => ''
));
echo $testimonials['small_title'];
echo $testimonials['main_title'];
echo $testimonials['description'];
echo isset($testimonials['small_title']) ? $testimonials['small_title'] : '';
echo isset($testimonials['main_title']) ? $testimonials['main_title'] : '';
echo isset($testimonials['description']) ? $testimonials['description'] : '';
我希望这将帮助您解决问题如果您在单个页面中使用它,它工作正常,如果您需要它在主页中工作,您需要在ACF功能中添加页面id,如
$testimonials = get_field('testimonials',the_ID);
谢谢你的回复我在这里写了什么,很抱歉我是Wordpress的初学者,我的知识在PHP$Estimationals=get_field('Estimationals',array('small_title'=>'here','main_title'=>'here','description'=>'here')中很简单;可以将其保留为空,也可以使用默认标题替换空值