Altered a function where should I put it?

Home Forums Calendar Products Events Calendar PRO Altered a function where should I put it?

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #129893
    Joel
    Participant

    I’ve altered the tribe_events_list_the_date_headers function in order to display the days in the loop as well as the month and year that is already displayed.

    My question is where should I put the altered function so that it’s saved when new releases come out and overrides/adds to the current function?

    my-theme/tribe-events/public/template-tags/loop.php?

    #130195
    Barry
    Member

    Hi – great question!

    Those functions aren’t overrideable quite in the same way as templates so the best approach is to use an appropriate filter, instead.

    In this case, the tribe_events_list_the_date_headers filter would work. So simply give your custom function, which could live in your theme’s functions.php file or even a dedicated plugin, its own name and hook it up like this:

    add_filter( 'tribe_events_list_the_date_headers', 'your_own_function_name' );

    Does that help here?

    #130386
    Joel
    Participant

    Great, thanks!

    #131786
    Joel
    Participant

    I’m guessing other people have/will be looking for a way to add dates to the calendar on the list page so here’s the function that I added in my theme’s function.php file:

    function add_date_to_events_calendar($html){
    $html = ”;
    global $post, $wp_query;
    $event_year = tribe_get_start_date( $post, false, ‘Y’ );
    $event_month = tribe_get_start_date( $post, false, ‘m’ );
    $event_day = tribe_get_start_date( $post, false, ‘j’ );

    if ($wp_query->current_post > 0) {
    $prev_post = $wp_query->posts[$wp_query->current_post – 1];
    $prev_event_year = tribe_get_start_date( $prev_post, false, ‘Y’ );
    $prev_event_month = tribe_get_start_date( $prev_post, false, ‘m’ );
    $prev_event_day = tribe_get_start_date( $prev_post, false, ‘j’ );
    }
    /*
    * If the event month changed since the last event in the loop,
    * or is the same month but the year changed.
    *
    */
    if ( $wp_query->current_post === 0 || ( $prev_event_month != $event_month || ( $prev_event_month == $event_month && $prev_event_year != $event_year ) ) ) {
    $html .= sprintf( “<span class=’tribe-events-list-separator-month’><span>%s</span></span>”, tribe_get_start_date( $post, false, ‘F Y’ ) );
    }
    /*
    * If this event year is different to the year of the previous event in the loop,
    * and it’s not it’s not the first event in the loop (we don’t want to start the loop with a year separator)
    */
    if ( $wp_query->current_post > 0 && $prev_event_year != $event_year ) {
    $html .= sprintf( “<span class=’tribe-events-list-separator-year’>%s</span>”, $event_year );
    }
    /*
    * If the event day changed since the last event in the loop,
    * or is the same day but the month changed.
    *
    */
    if ( $wp_query->current_post === 0 || ( $prev_event_day != $event_day || ( $prev_event_day == $event_day && $prev_event_month != $event_month ) ) ) {
    $html .= sprintf( “<span class=’tribe-events-list-separator-month’><span>%s</span></span>”, tribe_get_start_date( $post, false, ‘j l’ ) );
    }
    return $html;
    }
    add_filter( ‘tribe_events_list_the_date_headers’, ‘add_date_to_events_calendar’ );

    #135142
    Barry
    Member

    Thank you for sharing!

Viewing 5 posts - 1 through 5 (of 5 total)
  • The topic ‘Altered a function where should I put it?’ is closed to new replies.