Correcting Problems With Zero Speakers or Multiple Speakers in UUA Services Widget

ForumCategory: WidgetsCorrecting Problems With Zero Speakers or Multiple Speakers in UUA Services Widget
Scott asked 5 months ago

We noticed a few problems with the UUA Services featured and upcoming services widgets:

  • multiple speakers have no separation between them
  • if there are no speakers, then the date is not left-aligned with the service title and paragraph

To correct this, we offer the following changes. We delayed displaying the speaker’s “span” until we were sure that we had speakers, and when we did, we inserted commas between them with the implode() function. If there are no speakers, then we don’t display the speaker’s span at all, removing the space to the left of the date.
Note that in the following code snippets, we also removed the service times (because we often have more than 1 service), but you can keep those in as long as you don’t change the value passed into the_time() function on the last line of each of the following snippets.
As always, backup your code before making changes, and if possible, try to work on a staging site so as to limit disruptions to your production site
featured_upcoming_services_widget.php lines 48-59

    <p class="small"><?php
    global $post;
    $terms = wp_get_post_terms($post->ID, 'uu_service_speaker');
    $count = count($terms);
    if ( $count > 0 ) {
    $aNames = [];
        foreach ( $terms as $term ) {
        $aNames[] = $term->name;
        }
        echo '<span class="speaker">' . implode(", ", $aNames) . "</span>";
    }
  ?> <time class="" datetime="<?php echo get_the_time('c'); ?>"><?php the_time('F j'); ?></time></p>

upcoming_services_widget.php lines 53-64

    <p class="small"><?php
    global $post;
    $terms = wp_get_post_terms($post->ID, 'uu_service_speaker');
    $count = count($terms);
    if ( $count > 0 ) {
    $aNames = [];
        foreach ( $terms as $term ) {
        $aNames[] = $term->name;
        }
        echo '<span class="speaker">' . implode(", ", $aNames) . "</span>";
    }
    ?> <time class="" datetime="<?php echo get_the_time('c'); ?>"><?php the_time('F j'); ?></time></p>

 
Chris: If this code is in github, I’d be happy to update it there and submit a pull request.

1 Answers
cwjordan answered 3 months ago

Scott,

This was very helpful to me, thanks for posting it!