• Resolved Gary Wright

    (@garywright)


    Hey All!

    I have a bit of a PHP question about using an advanced custom field in a IF statement and then in a require_once.

    Here is my issue…

    I am using the following IF statement:

    <?php
    if(get_field('CUSTOM_FIELD'))
    {
    	echo '<p>' . get_field('CUSTOM_FIELD') . '</p>';
    }
    ?>

    However I would like to create a IF statement that will then trigger a require_once action that uses the custom field as the file name.

    Example:

    <?php
    require_once $_SERVER['DOCUMENT_ROOT'] . '/files/CUSTOM_FIELD.php';
    ?>

    Any guidance on this would be really appreciated!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    $field = get_field('CUSTOM_FIELD');
    if ($field) {
      $file = $_SERVER['DOCUMENT_ROOT'] . '/files/' . $field '.php';
      require_once( $file);
    }
    Thread Starter Gary Wright

    (@garywright)

    Thanks Steve!

    Works perfectly, I just added a missing full stop after the field and will share code below to help anyone in the future.

    $field = get_field('CUSTOM_FIELD');
    if ($field) {
      $file = $_SERVER['DOCUMENT_ROOT'] . '/files/' . $field . '.php';
      require_once( $file);
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Field, IF & Require_Once’ is closed to new replies.