|
<?php |
|
|
|
/** |
|
* Adds SK_Related_Case_Studies_Widget widget. |
|
*/ |
|
class SK_Related_Case_Studies_Widget extends WP_Widget { |
|
|
|
/** |
|
* Register widget with WordPress. |
|
*/ |
|
function __construct() { |
|
parent::__construct( |
|
'related_case_studies', // Base ID |
|
__( 'Related Case Studies', 'text_domain' ), // Name |
|
array( 'description' => __( 'Displays the latest 2 Case Studies belonging to the same category/categories that current single Post belongs to', 'text_domain' ), ) // Args |
|
); |
|
} |
|
|
|
/** |
|
* Front-end display of widget. |
|
* |
|
* @see WP_Widget::widget() |
|
* |
|
* @param array $args Widget arguments. |
|
* @param array $instance Saved values from database. |
|
*/ |
|
public function widget( $args, $instance ) { |
|
|
|
if ( ! is_singular( 'post' ) ) { |
|
return; |
|
} |
|
|
|
$categories = get_the_category(); |
|
|
|
$cat_slugs = ''; |
|
|
|
foreach( $categories as $category ) { // concatenate |
|
$cat_slugs .= ',' . $category->category_nicename; |
|
} |
|
|
|
// WP_Query arguments |
|
$args_related_case_studies = array ( |
|
'post_type' => 'case-study', |
|
'category_name' => $cat_slugs, |
|
'posts_per_page' => '2', |
|
); |
|
|
|
// The Query |
|
$query = new WP_Query( $args_related_case_studies ); |
|
|
|
// The Loop |
|
if ( $query->have_posts() ) { |
|
echo $args['before_widget']; |
|
if ( ! empty( $instance['title'] ) ) { |
|
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title']; |
|
} |
|
echo '<ul>'; |
|
while ( $query->have_posts() ) { |
|
$query->the_post(); ?> |
|
<!-- Display the Title as a link to the Post's permalink. --> |
|
<li> |
|
<a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a> |
|
|
|
<?php if ( has_post_thumbnail() ) { |
|
$image = genesis_get_image( 'format=url&size=carousel' ); |
|
printf( '<div class="related-case-study-image"><a href="%s" rel="bookmark"><img src="%s" alt="%s" /></a></div>', get_permalink(), $image, the_title_attribute( 'echo=0' ) ); |
|
} |
|
?> |
|
</li> |
|
<?php } |
|
echo '</ul>'; |
|
echo $args['after_widget']; |
|
} else { |
|
// no posts found |
|
return; |
|
} |
|
|
|
// Restore original Post Data |
|
wp_reset_postdata(); |
|
|
|
} |
|
|
|
/** |
|
* Back-end widget form. |
|
* |
|
* @see WP_Widget::form() |
|
* |
|
* @param array $instance Previously saved values from database. |
|
*/ |
|
public function form( $instance ) { |
|
$title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Case Studies', 'text_domain' ); |
|
?> |
|
<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> |
|
<?php |
|
} |
|
|
|
/** |
|
* Sanitize widget form values as they are saved. |
|
* |
|
* @see WP_Widget::update() |
|
* |
|
* @param array $new_instance Values just sent to be saved. |
|
* @param array $old_instance Previously saved values from database. |
|
* |
|
* @return array Updated safe values to be saved. |
|
*/ |
|
public function update( $new_instance, $old_instance ) { |
|
$instance = array(); |
|
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; |
|
|
|
return $instance; |
|
} |
|
|
|
} // class SK_Related_Case_Studies_Widget |
|
|
|
// register SK_Related_Case_Studies_Widget widget |
|
function sk_register_related_case_studies_widget() { |
|
register_widget( 'SK_Related_Case_Studies_Widget' ); |
|
} |
|
add_action( 'widgets_init', 'sk_register_related_case_studies_widget' ); |