0

I have a CPT with a set of fields set by ACF Pro - one of which is a date picker. I created a shortcode that can display these CPT's to a page and everything works except for the date. The date that shows through is the date of the first post created (it sets that date for all instances of the post-type instead of pulling through the date set for each individual entry)

I am trying to create a date object so that I can render the day and month above the year for stylistic purposes - I know I can just use the_field('date') otherwise but then I can't split the format up.

Can anyone see any issues or suggest how I can get this to show through?

//EVENTS SHORTCODE

add_shortcode('events', 'create_events_shortcode');
function create_events_shortcode ($atts){
    //merge the passed attributes with defaults
    extract(
        shortcode_atts(
            array(
                'post_type'         => 'event',
                'post_status'       => 'publish',
                'posts_per_page'    => 12,
            ),
            $atts
        )
    );

    // Custom Excerpt function for Advanced Custom Fields
    function custom_field_excerpt() {
        global $post;
        $text = get_field('event_description'); //Replace 'your_field_name'
        if ( '' != $text ) {
            $text = strip_shortcodes( $text );
            $text = apply_filters('the_content', $text);
            $text = str_replace(']]>', ']]>', $text);
            $excerpt_length = 20; // 20 words
            $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
            $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
        }
        return apply_filters('the_excerpt', $text);

    }

    // get raw date
    $date = get_field('date');


    // make date object
    $date = new Datetime($date);
    //now create a seprate agruments array for wp_query using the values from above(extracted variables)
    $args = array(
        'post_type'         => $post_type,
        'post_status'       => $post_status,
        'posts_per_page'    => $posts_per_page
    );

    ob_start();

    $my_query = new WP_Query( $args );
    if( $my_query->have_posts() ) {
        ?>
        <div id="nbf-event-list">
            <?php
            while ($my_query->have_posts()) : $my_query->the_post(); ?>

            <div class="individual-event"> 
                <div class="event-intro">
                    <div class="date-format">
                        <span class="date-details"><?php echo $date->format('jS F'); ?></span>
                        <span class="date-text"><?php echo $date->format('Y'); ?></span>
                    </div>  
                    <a href=" <?php the_permalink(); ?> "><h3 class="event-title"><?php the_title(); ?></h3></a>
                </div>

                <div class="main-event-box">                
                <div class="event-meta">
                    <h5>EVENT DETAILS</h5>
                    <span><strong>Start Time: </strong><?php the_field('start_time');?></span><br>
                    <span><strong>Event Type: </strong><?php the_field('event_type'); ?></span><br>
                    <span><strong>Location: </strong><?php the_field('location')?></span>
                </div>
                <div class="event-description">
                <p><?php echo custom_field_excerpt(); ?></p>
                </div>
                </div>

            </div>    
            <?php endwhile; ?>
        </div>
        <?php 
    }
    wp_reset_query();//reset the global variable related to post loop
    $retVal = ob_get_contents();
    ob_end_clean();

    return $retVal;
}
  • 1
    If you're trying to get the date for each post in the loop, then you need to move get_field inside the loop. – Milo Mar 4 at 16:40
  • @Milo when I do that I get a php error because it appears to parse the date incorrectly, am I retrieving it incorrectly? – Dan Sutherland Mar 4 at 20:52
  • As Milo said, move get_field inside the loop. Display the returned value to check format in which the date is stored. It looks like the date is not in the format accepted by the DateTime constructor. – nmr Apr 9 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.