Here is my widget to display the proper sunday service time based on the week of the month. WordPress is becoming my favorite CMS.
To display the widget I extend the widget function.
function widget($args, $instance){
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'] );
$nextSunday = strtotime("next sunday");
$wn = ceil( date( 'j', $nextSunday) / 7 );
$ldom = mktime(0, 0, 0, (date('m') + 1), 0, date('Y'));
$diff = round(abs($nextSunday-$ldom)/86400);
$text = "";
switch ($wn) {
case 1:
$text= $instance['Sunday1'];
break;
case 2:
$text= $instance['Sunday2'];
break;
case 3:
$text= $instance['Sunday3'];
break;
case 4:
if (diff>=7){
$text= $instance['Sunday4'];
}else {
$text= $instance['Sunday5'];
}
break;
case 5:
$text= $instance['Sunday5'];
break;
}?>
<?php echo $before_widget; ?>
<div class="clsPageContent">
<?php if ($title) { echo $before_title
. "<h3>"
. $title
. ""
. $after_title; } ?>
<ul>
<li>Sunday:</li>
<li>< ?php echo $text ?> </li>
<li> </li>
<li>< ?php echo $instance['MidWeekName'] ?> :</li>
<li>< ?php echo $instance['MidWeekTime'] ?> </li>
</ul><ul>
<p>
< ?php echo $instance['ExtraMessage'] ?>
</p>
</ul></div>
< ?php echo $after_widget; ?>
< ?php
}
Of course you need to have enter the information.
function form($instance){
$defaults = array( 'title' => 'Example',
'name' => 'John Doe',
'Sunday1' => '10 AM and 5 PM',
'Sunday2' => '2 PM and No Evening Service',
'Sunday3' => '10 AM and 5 PM',
'Sunday4' => '10 AM and 5 PM',
'Sunday5' => '10 AM and No Evening Service',
'MidWeekName' => 'Monday',
'MidWeekTime' => '7 PM',
'ExtraMessage' => ''
);
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"> Title:</label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
<br/>
<br/>
<label for="<?php echo $this->get_field_id( 'Sunday1' ); ?>"> First Sunday:</label>
<input id="<?php echo $this->get_field_id( 'Sunday1' ); ?>" name="<?php echo $this->get_field_name( 'Sunday1' ); ?>" value="<?php echo $instance['Sunday1']; ?>" style="width:100%;" />
<br/>
<label for="<?php echo $this->get_field_id( 'Sunday2' ); ?>"> Second Sunday:</label>
<input id="<?php echo $this->get_field_id( 'Sunday2' ); ?>" name="<?php echo $this->get_field_name( 'Sunday2' ); ?>" value="<?php echo $instance['Sunday2']; ?>" style="width:100%;" />
<br/>
<label for="<?php echo $this->get_field_id( 'Sunday3' ); ?>"> Third Sunday:</label>
<input id="<?php echo $this->get_field_id( 'Sunday3' ); ?>" name="<?php echo $this->get_field_name( 'Sunday3' ); ?>" value="<?php echo $instance['Sunday3']; ?>" style="width:100%;" />
<br/>
<label for="<?php echo $this->get_field_id( 'Sunday4' ); ?>"> Fourth Sunday:</label>
<input id="<?php echo $this->get_field_id( 'Sunday4' ); ?>" name="<?php echo $this->get_field_name( 'Sunday4' ); ?>" value="<?php echo $instance['Sunday4']; ?>" style="width:100%;" />
<br/>
<label for="<?php echo $this->get_field_id( 'Sunday5' ); ?>"> Fifth Sunday:</label>
<input id="<?php echo $this->get_field_id( 'Sunday5' ); ?>" name="<?php echo $this->get_field_name( 'Sunday5' ); ?>" value="<?php echo $instance['Sunday5']; ?>" style="width:100%;" />
<br/>
<br/>
<label for="<?php echo $this->get_field_id( 'MidWeekName' ); ?>"> Mid Week Service:</label>
<input id="<?php echo $this->get_field_id( 'MidWeekName' ); ?>" name="<?php echo $this->get_field_name( 'MidWeekName' ); ?>" value="<?php echo $instance['MidWeekName']; ?>" style="width:100%;" />
<br/>
<label for="<?php echo $this->get_field_id( 'MidWeekTime' ); ?>"> Mid Week Time:</label>
<input id="<?php echo $this->get_field_id( 'MidWeekTime' ); ?>" name="<?php echo $this->get_field_name( 'MidWeekTime' ); ?>" value="<?php echo $instance['MidWeekTime']; ?>" style="width:100%;" />
<br/>
<br/>
<label for="<?php echo $this->get_field_id( 'ExtraMessage' ); ?>"> Extra Message:</label>
<input id="<?php echo $this->get_field_id( 'ExtraMessage' ); ?>" name="<?php echo $this->get_field_name( 'ExtraMessage' ); ?>" value="<?php echo $instance['ExtraMessage']; ?>" style="width:100%;" />
<br/>
<?php
}
Here is the file to download and install:Sunday Widget