Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save celticwebdesign/287c02c03c8eb698326f83aa84d1dcbf to your computer and use it in GitHub Desktop.
Save celticwebdesign/287c02c03c8eb698326f83aa84d1dcbf to your computer and use it in GitHub Desktop.
// http://www.wpbeginner.com/wp-tutorials/how-to-create-a-custom-wordpress-widget/
//
// Select a Restaurant
class eoc_widget_select_restaurant extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'eoc_widget_select_restaurant',
// Widget name will appear in UI
__('Select a Restaurant', 'eatoutcornwall'),
// Widget description
array( 'description' => __( 'Widget to select a Restuarant', 'eatoutcornwall' ), )
);
}
// Creating widget front-end
// This is where the action happens
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
$number = apply_filters( 'widget_number', $instance['number'] );
if ( (! empty( $title ) ) && (strlen($number) != 0) ) {
// before and after widget arguments are defined by themes
echo $args['before_widget'];
echo "<div class='widget-select-restaurant clear'>";
echo $args['before_title'] . $title . $args['after_title'];
$query_args = array(
'post_type' => 'restaurants',
'p' => $number
);
// The query
$child_query = new WP_Query($query_args);
// The loop
if( $child_query->have_posts() ) : while ( $child_query->have_posts() ) : $child_query->the_post(); ?>
<!-- article -->
<article id="post-<?php the_ID(); ?>" class="clear" itemscope="" itemtype="http://schema.org/Restaurant">
<?php
echo "
<div class='thumbnail'>
<img src='".get_the_post_thumbnail_url( get_the_id(), 'eoc-thumbnail-300-200' )."' alt='".get_the_title()."' itemprop='image'>";
echo "<div class='textwidget'>
<h4>
<a href='".get_the_permalink()."' title='".get_the_title()."'>
<span itemprop='name'>"
.get_the_title().
"</span>
</a>
</h4>";
echo "
<span itemprop='address' itemscope='' itemtype='http://schema.org/PostalAddress'>
<span class='town' itemprop='addressLocality'>".get_field('business_town', get_the_id())."</span>
</span>
<span class='telephone' itemprop='telephone'>Tel: ".click_telephone( get_field('business_telephone', get_the_id()) )."</span>";
echo " </div>
</div>";
?>
</article>
<!-- /article -->
<?php endwhile; endif;
echo "</div>";
echo $args['after_widget'];
}
wp_reset_postdata();
}
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
} else {
$title = __( '', 'eatoutcornwall' );
}
if ( isset( $instance[ 'number' ] ) ) {
$number = $instance[ 'number' ];
} else {
$number = __( '', 'eatoutcornwall' );
}
// Widget admin form
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<p>
<?php
$query_args = array(
'post_type' => 'restaurants',
'posts_per_page' => -1,
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'meta_key' => 'business_paid_listing',
'meta_value' => true
);
// The query
$child_query = new WP_Query($query_args);
echo '<select name="'.$this->get_field_name( 'number' ).'">';
if( $child_query->post_count === 0 ) {
echo '<option value="0">No PAID Restaurants</option>';
} else {
echo '<option value="0">Select Restaurant</option>';
// The loop
if( $child_query->have_posts() ) : while ( $child_query->have_posts() ) : $child_query->the_post();
echo '<option value="'.get_the_id().'" ';
if( $number == get_the_id() ) {
echo 'selected';
}
echo '>'.get_the_title().'</option>';
endwhile; endif;
}
echo "</select>";
wp_reset_postdata();
?>
</p>
<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['number'] = ( ! empty( $new_instance['number'] ) ) ? strip_tags( $new_instance['number'] ) : '';
return $instance;
}
} // Class celtic_widget_posts ends here
// Register and load the widget
function wpb_load_widget_restaurant() {
register_widget( 'eoc_widget_select_restaurant' );
}
add_action( 'widgets_init', 'wpb_load_widget_restaurant' );
// Select a Restaurant
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment