Similar to action hooks that we have seen in the earlier chapters, the widget method is meant to directly output HTML code to the browser that will be displayed when an instance of the new widget has been created in a sidebar.
The widget method starts by checking whether or not the widget should be displayed. If it should, it continues by calling the standard PHP extract function on the first parameter received, an array named $args. Calling this function parses the array and creates variables for each element found, making it easier for the following code to access the elements that should be placed before and after the widget title and widget content.
After this initial statement, the recipe continues by retrieving the number of items to display and the widget title from the $instance array, which has been received as the second method parameter using the same technique that was shown when implementing the form method.
The rest of the code is very similar to the book review shortcode created in Chapter 4, The Power of Custom Post Types (displaying custom post type data in shortcodes), where we assemble a query string that indicates the type and maximum quantity of data that we want to retrieve from the database. The resulting query is executed by creating a new instance of the WordPress WP_Query object. If results are found, the following recipe code cycles through all the entries and outputs code to render an unordered list of all the items found. Last, but not least, the recipe formats the widget content by outputting the values of the $before_widget, $after_widget, $before_title, and $after_title widget class variables, and the user-specified widget title in the right places.