Support

Account

Home Forums General Issues Help: How to create field Expired – Current Reply To: Help: How to create field Expired – Current

  • Hello,

    As long as you have the field in question setup with ACF, you are set on that end of things.

    It’s then just a matter of displaying the correct data on the front-end to your Users, based on the date in that field (compared to today’s date).

    There are several ways to accomplish it… here are 2 ideas that came to me:

    1) Using PHP (probably the easiest)

    Here are the basic steps:

    a) Create an HTML/CSS template for what you want to accomplish. Something like below (based on your screenshot):

    
    <ul>
    <li><strong>Employment Status:</strong> <span style="color:green;">Current</a></li>
    <li><strong>This job will expire:</strong> December 25, 2017</li>
    </ul>
    

    b) Now, within your code, you will want to re-create the above dynamically. You can do something like (perhaps in a loop if you have multiple records to display):

    
    <?php
    $expiry_date = get_field('expiry_date');
    $is_expired = FALSE;
    $status_text = 'Current';
    $status_text_2 = 'This job will expire on:';
    $status_color = 'green';
    if ( strtotime(date('Y-m-d')) > date('Y-m-d', strtotime($expiry_date)) ) {
      $is_expired = TRUE;
      $status_text = 'Expired';
      $status_text_2 = 'This job expired on:';
      $status_color = 'red';
    }
    echo '<ul>';
    echo '<li><strong>Employment Status:</strong> <span style="color:' . $status_color . ';">' . $status_text . '</a></li>';
    echo '<li><strong>' . $status_text_2 . '</strong> ' . date("F j, Y", strtotime($expiry_date)) . '</li>';
    echo '</ul>';
    ?>
    

    Resources:

    2) Using vanilla JavaScript (or JQuery).

    Here are the basic steps:

    a) Print the expiration date to the page, perhaps within a ‘list item’ tag of an unordered list. The unordered list should have a unique ID, so that you can ‘select’ it later with JavaScript.

    b) Within the ‘list item’ tag, you can have a ‘paragraph’ tag to display your desired message, namely: Current or Available

    c) In the JavaScript (that you add to the footer of the web page), “select” the ‘unordered list’, then iterate through the ‘list items’, check if the date has passed, and set the HTML of the ‘paragraph’ tag appropriately.