Working with the Popular Articles Widget

This widget shows a number of articles from all knowledge bases based on their number of likes.

[iconheading type=”h2″ style=”glyphicon-cog”]Widget Settings[/iconheading]

Popular Articles Widget Settings
Popular Articles Widget Settings

Simply go to Appearance Widgets and drag and drop the widget to your desired location. The settings will ask you for the title and the number of articles to show.

[iconheading type=”h2″ style=”glyphicon-eye-open”]Widget Output[/iconheading]

Popular Articles Widget Output
Popular Articles Widget Output

The screenshot above shows the output of the widget.

[iconheading type=”h2″ style=”glyphicon-plus”]Extending the Widget[/iconheading]

The argument that goes inside the WP_Query instantiation is filtered. So you can change it from outside using a plugin. The usage is something like this.

[php]
/**
* Modify the popular widget query parameters
*
* @see http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
* @param array $arg The default query parameters
* @return array Modified query parameters.
*/
function my_popular_widget_ext( $arg ) {
// Conditionally add the category parameter
// for category archive pages
// This will make the popular widget to show articles
// only under the current category
if ( is_category() ) {
$arg[‘cat’] = get_query_var( ‘cat’ );
}
return $arg;
}
add_filter( ‘ipt_kb_popular_widget_query_args’, ‘my_popular_widget_ext’ );
[/php]

The widget class is located under /inc/class-ipt-kb-popular-widget.php.