Working with the Popular Articles Widget

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

Widget Settings

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.

Widget Output

Popular Articles Widget Output

Popular Articles Widget Output

The screenshot above shows the output of the widget.

Extending the Widget

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.

/**
 * 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' );

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

Swashata has written 257 articles

Hi there, I am the Lead Developer at WPQuark.com. I love to create something beautiful for WordPress and here I write about how to use them. When I am not working, usually I am doing a number of other creative things ;).