【问题标题】:Wordpress Advanced Custom Fields display content if no option如果没有选项,Wordpress 高级自定义字段会显示内容
【发布时间】:2018-06-15 13:11:39
【问题描述】:

根据用户是否从帖子对象字段中选择页面 ID,我有一些选项可以在页脚中显示不同的地址。我让它工作,如果用户选择 post object = 'Music' 它会显示 'Music' 地址等,但我无法工作的是,如果没有选择任何选项,则只显示默认地址。

这是我目前所拥有的:

<?php if( have_rows('footer_details', 'option') ): ?>
    <?php while( have_rows('footer_details', 'option') ): the_row(); ?> 
        <?php
            $post_object = get_sub_field('company', 'option');
        ?>
        <?php
            if ( is_page($post_object) ) {  ?>
            <div class="company-footer">
                <?php echo get_sub_field('address', 'option'); ?>
            </div>
        <?php } 
        ?>
    <?php endwhile; ?>
<?php endif; ?>

<p><?php echo get_field('footer_address', 'option'); ?></p>

【问题讨论】:

  • 默认地址是什么?那是存储在某个地方吗?我们怎样才能找回它?
  • 默认地址只是一个名为footer_address的ACF选项,它不在repeater字段中。
  • 明白,但您要求我们帮助您显示默认地址,但没有向我们展示了如何获取/显示默认地址。所以根据定义,你得到的任何答案都是不完整的。
  • 我使用默认页脚地址字段编辑了我的原始问题。但是,即使我从转发器字段中添加了页脚,这也会显示在每一页上。

标签: php wordpress advanced-custom-fields


【解决方案1】:

您要做的是检查是否设置了address 字段,如果没有设置,则显示default 地址。

一种方法如下所示 - 我已将地址加载到变量中,如果变量为“空”,则将默认地址分配给变量。

注意:
在下面的代码中,我做了两件事:
我已经删除了很多打开/关闭 PHP 标记。除了从 PHP 转换到 HTML 时,这些都不是必需的,并且会使代码更加“嘈杂”且难以阅读。

我添加了 cmets 来解释我在做什么。如果需要,您可以安全地移除这些 cmets。

<?php // load footer address outside of repeater
      $default = get_field('footer_address', 'option');
      if( have_rows('footer_details', 'option') ):
          // removed closing / opening PHP tags for simplicity
          while( have_rows('footer_details', 'option') ): 
              // moved the_row to it's own line for clarity
              the_row();
              $post_object = get_sub_field('company', 'option');
              if ( is_page($post_object) ) { 
                  // load the address custom field into a variable named $address
                  $address = get_sub_field('address', 'option');
                  // check if the $address variable is empty
                  if ( ! $address ) {
                      // if it is empty, load the default into the $address variable
                      $address = $default;
                  } ?>
                  <div class="company-footer">
                      <?php // output the $address variable, which contains either the address or the default address
                            echo $address; ?>
                    </div>
            <?php } 
          endwhile;
       endif; ?>

【讨论】:

  • 感谢您的回复,首页没有显示页脚地址(默认地址)。 footer_address 字段是否必须在转发器中?转发器中的其他页脚在所选页面上正常工作。
  • 检查我刚刚所做的编辑。调整为在页脚之外加载默认地址。
  • 谢谢,但默认页脚不显示...你可以在这里看到foley13.webfactional.com/sky/home
  • 这就是为什么我要求您展示我们应该如何显示默认页脚。我绝对不可能猜到如何从系统中获取您的默认页脚并显示它 - 那么,您能否发布 工作 以显示您的默认页脚的代码?
  • 如果我只有 这会在每个页面上显示默认的网站页脚。
【解决方案2】:

玩了很久终于解决了

<?php // load footer address outside of repeater

        $exists = false; //initialize the field to false;
        $default = get_field('footer_address', 'option');
        if( have_rows('footer_details', 'option') ):
          while( have_rows('footer_details', 'option') ): 
            the_row(); // moved the_row to it's own line for clarity
            $post_object = get_sub_field('company', 'option');
              if ( is_page($post_object) ) { 
                // load the address custom field into a variable named $address
                $exists = true; //set the value to true;
                $address = get_sub_field('address', 'option');
                // check if the $address variable is empty
              if ( ! $address ) {
                // if it is empty, load the default into the $address variable
                $address = $default;
              } 
        ?>

        <div class="company-footer">
          <?php 
            echo $address; // output the $address variable, which contains either the address or the default address
            echo get_sub_field('contact','option'); 
          ?>
        </div>

      <?php } 

        endwhile;
        endif;
        // evalute if it existed
        if($exists==false){ ?> 
        <div class="company-footer">
          <?php echo 'use the default footer'; ?>
        </div>
      <?php }
    ?>

【讨论】:

    猜你喜欢
    • 2021-06-04
    • 1970-01-01
    • 1970-01-01
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多