我在WordPress项目中工作,我在一个名为testimonials.php的特殊文件中创建了推荐信,我通过get_template_part在页面上调用了关于我们的文件,它工作得很好,但是当我调用(testimonials.php)到主页时,没有显示输入的所有部分。
使用高级自定义字段插件
这段代码
<!-- 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>并非从所有输入中出现的部分。
<span> <? php echo $ testimonials ['small_title']; ?> </ span>
<h2> <? php echo $ testimonials ['main_title']; ?> </ h2>
<? php echo $ testimonials ['description']; ?>发布于 2019-06-15 23:35:47
如果您在单个页面中使用了该功能,则它运行良好,如果您需要它在主页中工作,则需要在ACF函数中添加页面id,如
$testimonials = get_field('testimonials',the_ID);发布于 2019-06-13 00:31:17
1 -根据您的代码get_field(‘get_field’)需要是一个数组,所以最好的方法是将默认值数组给这个函数,例如:。
$testimonials = get_field('testimonials', array(
'small_title' => '',
'main_title' => '',
'description' => ''
));因此,通过这种方式,如果字段不是数组或null,则设置默认值,因为有时如果数据库中没有记录当前post的数据,则此函数将返回null。
2 -下面的字符串通过给定的键获取数组的$testimonials值:
echo $testimonials['small_title'];
echo $testimonials['main_title'];
echo $testimonials['description'];但是,如果数组$testimonials中不存在密钥呢?您将需要使用PHP函数isset()和简写条件if语句来避免警告消息或破坏html代码。
这是正确的方法:
echo isset($testimonials['small_title']) ? $testimonials['small_title'] : '';
echo isset($testimonials['main_title']) ? $testimonials['main_title'] : '';
echo isset($testimonials['description']) ? $testimonials['description'] : '';我希望这能帮你解决这个问题。
https://stackoverflow.com/questions/56570943
复制相似问题