* @package AnsPress/AP_Related_questions */ /** * Extend WP_Widget. */ class AP_Related_questions extends WP_Widget { /** * Initialize the class */ public function __construct() { parent::__construct( 'ap_related_questions', __( '(AnsPress) Related Questions', 'ap' ), array( 'description' => __( 'For showing related question. This widget will only work in question page.', 'ap' ) ) ); } public function widget( $args, $instance ) { global $questions; $title = apply_filters( 'widget_title', $instance['title'] ); echo $args['before_widget']; if ( ! empty( $title ) ) { echo $args['before_title'] . $title . $args['after_title']; } echo '
'; if ( ! class_exists( 'Tags_For_AnsPress' ) ) { echo 'Tags plugin must be installed for related question. Get Tags for AnsPress'; return; } $tags = get_the_terms( get_question_id(), 'question_tag' ); $tags_in = array(); if ( $tags ) { foreach ( $tags as $t ) { $tags_in[] = $t->term_id; } } $question_args = array( 'tax_query' => array( array( 'taxonomy' => 'question_tag', 'field' => 'term_id', 'terms' => $tags_in, ), ), 'showposts' => 5, 'post__not_in' => array( get_question_id() ), ); $questions = ap_get_questions( $question_args ); include ap_get_theme_location( 'widget-related_questions.php' ); wp_reset_postdata(); echo '
'; echo $args['after_widget']; } public function form( $instance ) { $title = __( 'Related Questions', 'ap' ); if ( isset( $instance[ 'title' ] ) ) { $title = $instance[ 'title' ]; } ?>