【问题标题】:check if function exist advanced custom fields检查函数是否存在高级自定义字段
【发布时间】:2015-07-14 11:00:16
【问题描述】:

我无法提前定义自定义字段的函数。 基本上我需要一个由标题(文本字段)填充的字段,一个所见即所得的编辑器和几个产品页面底部的两个文件上传字段。 但我只想在填充其中一个字段时显示字段+标题。

产品信息字段是:

product_info_title - 文本字段

product_info_content - 所见即所得编辑器

product_info_file_1 - 文件

product_info_file_2 - 文件

这是我的自定义页面模板中当前的 acf 代码:

<div class="post-content">
    <?php the_content(); ?>
        <?php 
        if( function_exists('get_field')) {
        if(get_field('product_info_content')) {
        echo '<div class="info-box">';
        echo '<h1>' . get_field('product_info_title') . '</h1>';
        the_field('product_info_content');
        echo '</div>';
        }
        }
        ?>

我无法正常工作的是检查是否填充了除标题字段之外的三个字段中的任何一个,如果是,则将其与标题字段一起显示。

【问题讨论】:

  • 如果我错了请纠正我。您当前要检查三个字段(product_info_content、product_info_file_1、product_info_file_2)之一是否已填充,您将显示 product_info_title 字段。如果我认为是对的。也许你可以使用 javascript 来归档这个问题。

标签: wordpress advanced-custom-fields


【解决方案1】:

因此,如果填充了一个字段 OR 另一个字段,则您想显示一些内容,那么您需要在您的条件中使用 OR

if( field1 OR field2 OR field3 ) {
   // show something
}

应用于您的代码,条件将类似于:

if ( get_field('product_info_content') OR get_field('product_info_file_1')  OR get_field('product_info_file_2') ) {
    // echo the <div>
}

【讨论】:

  • 可以填充三个字段中的任何一个,在所见即所得的编辑器中可能需要一些文本或图像,或者只有技术数据 pdf 附件,当根本没有产品信息时,信息框字段也不应该出现。
  • 尝试此代码,但自定义页面模板页面在尝试回显任何 pdf 附件时崩溃。
猜你喜欢
  • 1970-01-01
  • 2013-10-20
  • 2017-12-16
  • 2014-02-22
  • 2016-07-17
  • 2012-10-17
  • 2017-08-26
  • 1970-01-01
  • 2013-07-14
相关资源
最近更新 更多