• Resolved lieska

    (@lieska)


    Hi and thanks for the plugin! Nice and easy in backend.

    Now with my poor PHP knowledge I’m trying to show a post list where there are normal posts and event posts. I want to show the event date in its own box, and not show the box at all when there is no event date. Check https://tinyurl.com/jmpjcoz

    I tried this IF sentence, but the box is there even though it has no value. I also tried it with isset: if (isset($mem_date)), but the result was the same, empty box was there when no event date.

    <?php
    $mem_date = mem_date_processing(get_post_meta($post->ID, ‘_mem_start_date’, true));
    if (!empty($mem_date)){
    echo ‘<div class=”meta-info”><div class=”meta-date”><span class=”month”>’;
    echo date_i18n( ‘M’, $mem_date[“start-unix”] );
    echo ‘</span><span>’;
    echo date_i18n( ‘d’, $mem_date[“start-unix”] );
    echo ‘</span></div></div>’;
    }
    ?>

    • This topic was modified 8 years, 6 months ago by lieska.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Manuel Schmalstieg

    (@targz-1)

    Thanks for your feedback!

    You will find an example of what you want to achieve in the wiki: https://github.com/ms-studio/minimalistic-event-manager/wiki/How-to-display-the-dates

    Your code is almost fine, the thing that prevents it from working is that when you define $mem_date, via the mem_date_processing() function, you get an array full of information. So even if the fields are empty, you still get an array.

    So you could either test:

    a) If get_post_meta returns content.
    b) If a specific entry of the $mem_date array has content – for example $mem_date[“start-iso”] which holds exactly the same content as the ‘_mem_start_date’ field.

    An example for (a) would be:

    $mem_start_date = get_post_meta($post->ID, '_mem_start_date', true);
    if ($mem_start_date !== "" ) {
      echo '<p>start date: '.$mem_start_date.'</p>';
    } else {
      // nothing to show
    }

    An example for (b) would be:

    // First, check if date fields are present.
    // This will return an array with formatted dates.
    $mem_date = mem_date_processing( 
        get_post_meta($post->ID, '_mem_start_date', true) , 
        get_post_meta($post->ID, '_mem_end_date', true)
    );
    
    // Second step: display the date
    if ($mem_date["start-iso"] !=="") { // show the event date
        echo '<div class="event-date">';
        echo $mem_date["date"];
        echo '</div>';
    } else { // no event date? - show the publication date
        ?>
        <div class="pub-date">Posted on <?php the_time('j F'); ?></div>
        <?php
    }

    I hope this helps ??

    Thread Starter lieska

    (@lieska)

    Hi Manuel, thank you, this really helped!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Wrap the date only if there is a value’ is closed to new replies.