0

I'm currently ordering a bunch of events based on a datepicker from ACF. They are all grouped per month, so all events of October are all listed together, than November, December and on. I only have one problem with the formatting of the datemonth.

Before each new month the name of the month is in a , but the name of the month is in English, while I would want it to be in Dutch (because of the website is Dutch and so is the WordPress install. I have tried changing:

$month = $date->format('F');

To

$month = $date->echo date_i18n('F');

But this is not what it should be and I'm not sure to what to change it to. Below is the full query / grouping of the posts, but if someone could give me the final shot on how to display the name of the month correctly, much appreciated.

<?php
$posts = get_posts(array(
    'numberposts'   => -1,
    'cat'           => '-1',
    'order'         => 'ASC',
    'orderby'       => 'meta_value',
    'meta_key'      => '_expiration-date',   
));

$group_posts = array();
    if( $posts ) {

    foreach( $posts as $post ) {
        $date = get_field('datum-concert', $post->ID, false);

        $date = new DateTime($date);
        $year = $date->format('Y');
        $month = $date->format('F');
        // $month = $date->echo date_i18n('F');

        $group_posts[$year][$month][] = array($post, $date);
    }}

    foreach ($group_posts as $yearKey => $years) {
    foreach ($years as $monthKey => $months) {
        echo '<h2 style="text-transform: capitalize;">';
        echo $monthKey;
        echo ' ';
        echo $yearKey;
        echo '</h2>';

    foreach ($months as $postKey => $posts) { ?>
        <?php while ( have_posts() ) : the_post(); ?>
        <?php get_template_part( 'concert-single' ); ?>
        <?php endwhile;
    }}}
?> 
0

Here you were trying to use echo as a method of DateTime object.

$month = $date->echo date_i18n('F');

It should be:

$month = date_i18n('F', strtotime( $acf_date ) );

Where $acf_date is the time string not the DateTime object.

  • Hi Greg, The change does infact display the name of the month correctly, but it does break the wrapper for each month. It now only displays 1 (the current) month. All other events, that are in november, december and so one are all listed, but the wrapping with the <h2>+ name of the month are gone. – imrafaelhi Oct 19 '17 at 14:27
  • You are using month names as table keys so you probably are passing the wrong time string to the strtotime( $acf_date ). Try this: $month = date_i18n('F', strtotime( get_field('datum-concert', $post->ID, false) ) ); – Greg36 Oct 19 '17 at 14:43
  • Thank you! Finally a solution for this. It has been on my to-do list forever but had trouble finding a solution. Thank you so much! – imrafaelhi Oct 19 '17 at 19:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.