• Resolved netschmiede24

    (@netschmiede24)


    Hi,

    I have a pod with a repeatable date field. The field is called “termine”. It outputs the array being shown like this “Date 1, Date 2, Date 3 and Date 4”.

    Is there a simple way in the template to output the first date, the last date and the number of dates in this array, as in “Date 1 to Date 4 (4 Dates)” ?

    Thanks,
    Astrid

Viewing 1 replies (of 1 total)
  • Plugin Support pdclark

    (@pdclark)

    <?php
    /**
    * Plugin Name: Date Count Shortcode
    * Description: <code>[date_count for="termine"]</code>
    */
    add_shortcode(
    'date_count',
    function( $args, $content, $tag ) {
    $dates = (array) get_post_meta( get_the_ID(), $args['for'], false );

    if ( empty( $dates ) ) {
    return __( '<p>(0 Dates)</p>' );
    }

    $count = (int) count( $dates );
    return sprintf(
    '<p>%s%s (%s)</p>',
    date_i18n(
    get_option( 'date_format' ),
    strtotime( $dates[0] )
    ),
    array_key_exists( 1, $dates )
    ? sprintf(
    ' to %s',
    date_i18n(
    get_option( 'date_format' ),
    strtotime( end( $dates ) )
    )
    )
    : '',
    sprintf(
    _n(
    '%s Date',
    '%s Dates',
    $count
    ),
    number_format_i18n( $count )
    )
    );
    }
    );
Viewing 1 replies (of 1 total)

You must be logged in to reply to this topic.