Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * WPSE Category Specific Widget
- *
- * This widget will change post according to the current category being viewed.
- *
- * @package WPSE Category Specific Widget
- * @author Pieter Goosen
- * @license GPL-2.0+
- * @requires PHP 5.4+
- * @requires Categories Images Plugin https://wordpress.org/plugins/categories-images/
- * @ref http://wordpress.stackexchange.com/q/158337/31545
- *
- */
- class WPSE_Category_Specific_Widget extends WP_Widget {
- /**
- * wpse-category-specific-widget
- *
- * Unique identifier for your widget.
- *
- *
- * The variable name is used as the text domain when internationalizing strings
- * of text. Its value should match the Text Domain file header in the main
- * widget file.
- *
- * @since 1.0.0
- *
- * @var string
- */
- protected $widget_slug = 'wpse-category-specific-widget';
- /*--------------------------------------------------*/
- /* Constructor
- /*--------------------------------------------------*/
- /**
- * Specifies the class name and description, instantiates the widget,
- * loads localization files, and includes necessary stylesheets and JavaScript.
- */
- public function __construct() {
- parent::__construct(
- $this->get_widget_slug(),
- __( 'WPSE Category Specific Widget', $this->get_widget_slug() ),
- array(
- 'classname' => $this->get_widget_slug().'-class',
- 'description' => __( 'This widget will change post according to the current category being viewed.', $this->get_widget_slug() )
- )
- );
- // Refreshing the widget's cached output with each new post
- add_action( 'save_post', array( $this, 'flush_widget_cache' ) );
- add_action( 'deleted_post', array( $this, 'flush_widget_cache' ) );
- add_action( 'switch_theme', array( $this, 'flush_widget_cache' ) );
- } // end constructor
- /**
- * Return the widget slug.
- *
- * @since 1.0.0
- *
- */
- public function get_widget_slug() {
- return $this->widget_slug;
- }
- /*--------------------------------------------------*/
- /* Widget API Functions
- /*--------------------------------------------------*/
- /**
- * Outputs the content of the widget.
- *
- * @param array args The array of form elements
- * @param array instance The current instance of the widget
- */
- public function widget( $args, $instance ) {
- // Check if there is a cached output
- $cache = wp_cache_get( $this->get_widget_slug(), 'widget' );
- if ( !is_array( $cache ) )
- $cache = array();
- if ( ! isset ( $args['widget_id'] ) )
- $args['widget_id'] = $this->id;
- if ( isset ( $cache[ $args['widget_id'] ] ) )
- return print $cache[ $args['widget_id'] ];
- // go on with your widget logic, put everything into a string and …
- extract( $args, EXTR_SKIP );
- $widget_string = $before_widget;
- ob_start();
- if( is_category() || is_tax() ) {
- // Get the image from the category or taxonomy term
- if (function_exists('z_taxonomy_image_url')) {
- echo '<p><img src="' . z_taxonomy_image_url() . '"/></p>';
- }
- // Create the custom query
- $queried_object = get_queried_object( 'term' );
- $taxonomy = $queried_object->taxonomy;
- $term = $queried_object->term_id;
- $args = [
- 'posts_per_page' => 1,
- 'order' => 'DESC',
- 'orderby' => 'rand',
- 'tax_query' => [
- [
- 'taxonomy' => $taxonomy,
- 'field' => 'term_id',
- 'terms' => $term
- ]
- ]
- ];
- }elseif( !is_404() ) {
- $args = [
- 'post_type' => 'any',
- 'posts_per_page' => 1,
- 'orderby' => 'rand'
- ];
- }
- $q = new WP_Query( $args );
- if($q->have_posts()) {
- while($q->have_posts()) {
- $q->the_post(); ?>
- <p><?php the_title(); // Display post title ?></p>
- <?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
- the_post_thumbnail();
- } ?>
- <p><?php the_excerpt(); // Show an excerpt of the post ?></p>
- <?php }
- }
- wp_reset_postdata();
- $widget_string .= ob_get_clean();
- $widget_string .= $after_widget;
- $cache[ $args['widget_id'] ] = $widget_string;
- wp_cache_set( $this->get_widget_slug(), $cache, 'widget' );
- print $widget_string;
- } // end widget
- public function flush_widget_cache()
- {
- wp_cache_delete( $this->get_widget_slug(), 'widget' );
- }
- /**
- * Processes the widget's options to be saved.
- *
- * @param array new_instance The new instance of values to be generated via the update.
- * @param array old_instance The previous instance of values before the update.
- */
- public function update( $new_instance, $old_instance ) {
- $instance = $old_instance;
- // TODO: Here is where you update your widget's old values with the new, incoming values
- return $instance;
- } // end widget
- /**
- * Generates the administration form for the widget.
- *
- * @param array instance The array of keys and values for the widget.
- */
- public function form( $instance ) {
- // TODO: Define default values for your variables
- $instance = wp_parse_args(
- (array) $instance
- );
- } // end form
- } // end class
- add_action( 'widgets_init', function(){
- register_widget( 'WPSE_Category_Specific_Widget' );
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement