【问题标题】:get_template_part() not working with Advanced Custom Fields pluginget_template_part() 不适用于高级自定义字段插件
【发布时间】:2019-06-16 07:35:47
【问题描述】:

我在 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']; ?>

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    如果你在单个页面中使用它,它工作得很好,如果你需要它在主页上工作,你需要在 ACF 函数中添加页面 id,比如

    $testimonials = get_field('testimonials',the_ID);
    

    【讨论】:

      【解决方案2】:

      1 - 根据您的代码 get_field('testimonials') 需要是一个数组,因此最好的方法是为此函数提供默认值数组,例如:

      $testimonials = get_field('testimonials', array(
          'small_title' => '',
          'main_title' => '',
          'description' => ''
      ));
      

      因此,如果字段不是数组或空值,则通过这种方式设置默认值,因为有时如果当前帖子的数据库中没有记录数据,此函数将返回空值。

      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'] : '';
      

      我希望这能帮助您解决问题

      【讨论】:

      • 谢谢你的回复我在这里放什么,对不起这个问题我是Wordpress的初学者,我的PHP知识很简单 $ testimonials = get_field ('testimonials', array ( 'small_title' => '这里', 'main_title' => '这里', '描述' => '这里' ));
      • 您可以将其留空或使用默认标题替换空值
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-25
      • 1970-01-01
      • 2018-07-11
      • 2017-12-16
      • 2017-04-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多