Last active
September 21, 2017 02:12
-
-
Save patric-boehner/199417ec5fb3d30c4e51f185bdd882a9 to your computer and use it in GitHub Desktop.
Recent Post Excerpt Widget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Recent post excerpt widget | |
| * | |
| * @package CoreFunctionality | |
| * @author Patrick Boehner <[email protected]> | |
| * @since 1.0.1 | |
| * @copyright Copyright (c) 2016, Patrick Boehner | |
| * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License | |
| * | |
| */ | |
| //* Block Acess | |
| //********************** | |
| if( !defined( 'ABSPATH' ) ) exit; | |
| //* Widget | |
| //********************** | |
| class PB_Recent_Post_Widget extends WP_Widget { | |
| /** | |
| * Holds widget settings defaults, populated in constructor. | |
| * | |
| * @since 1.0.0 | |
| * @var array | |
| */ | |
| protected $defaults; | |
| /** | |
| * Constructor | |
| * | |
| * @since 1.0.0 | |
| */ | |
| function __construct() { | |
| // widget defaults | |
| $this->defaults = array( | |
| 'title' => '', | |
| 'post_category' => '', | |
| ); | |
| // Widget Slug | |
| $widget_slug = 'pb-recent-post-excerpt-widget'; | |
| // widget basics | |
| $widget_ops = array( | |
| 'classname' => $widget_slug, | |
| 'description' => 'Display the most recent post from a cateogry you select' | |
| ); | |
| // widget controls | |
| $control_ops = array( | |
| 'id_base' => $widget_slug, | |
| // 'width' => '400', | |
| ); | |
| // load widget | |
| parent::__construct( $widget_slug, 'Recent Post Excerpt', $widget_ops, $control_ops ); | |
| } | |
| /** | |
| * Outputs the HTML for this widget. | |
| * | |
| * @since 1.0.0 | |
| * @param array $args An array of standard parameters for widgets in this theme | |
| * @param array $instance An array of settings for this widget instance | |
| */ | |
| function widget( $args, $instance ) { | |
| extract( $args ); | |
| // Merge with defaults | |
| $instance = wp_parse_args( (array) $instance, $this->defaults ); | |
| echo $before_widget; | |
| //* Title | |
| if ( !empty( $instance['title'] ) ) { | |
| echo $before_title . apply_filters( 'widget_title', $instance['title'] ) . $after_title; | |
| } | |
| //* Post Query Args | |
| $post_args = array( | |
| 'post_type' => 'post', | |
| 'posts_per_page' => '1', | |
| 'cat' => $instance['post_category'], | |
| ); | |
| //* Post Query | |
| $post_query = null; | |
| $post_query = new WP_Query( $post_args ); | |
| if( $post_query->have_posts() ) { | |
| while ( $post_query->have_posts() ) : | |
| $post_query->the_post(); | |
| $post_title = esc_html( get_the_title() ); | |
| $post_link = esc_url( get_the_permalink() ); | |
| $button_text = esc_html( 'Continue Reading' ); | |
| $screen_reader = '<span class="screen-reader-text"> ' .$post_title. '</span>'; | |
| echo '<article class="recent-post">'; | |
| echo '<header class="entry-header"><h4 class="entry-title" itemprop="headline">' .$post_title. '</h4></header>'; | |
| echo '<div class="entry-content" itemprop="text">'; | |
| echo the_excerpt(); | |
| echo '<a class="button" href="' .$post_link. '" itemprop="url">' .$button_text. '' .$screen_reader. '</a>'; | |
| echo '</div></article>'; | |
| endwhile; | |
| wp_reset_postdata(); | |
| } | |
| echo $after_widget; | |
| } | |
| /** | |
| * Deals with the settings when they are saved by the admin. Here is | |
| * where any validation should be dealt with. | |
| * | |
| * @since 1.0.0 | |
| * @param array $new_instance An array of new settings as submitted by the admin | |
| * @param array $old_instance An array of the previous settings | |
| * @return array The validated and (if necessary) amended settings | |
| */ | |
| function update( $new_instance, $old_instance ) { | |
| $new_instance['title'] = strip_tags( $new_instance['title'] ); | |
| $new_instance['post_category'] = esc_html( $new_instance['post_category'] ); | |
| return $new_instance; | |
| } | |
| /** | |
| * Displays the form for this widget on the Widgets page of the WP Admin area. | |
| * | |
| * @since 1.0.0 | |
| * @param array $instance An array of the current settings for this widget | |
| */ | |
| function form( $instance ) { | |
| // Merge with defaults | |
| $instance = wp_parse_args( (array) $instance, $this->defaults ); | |
| ?> | |
| <p> | |
| <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label> | |
| <input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_html( $instance['title'] ); ?>" class="widefat" /> | |
| </p> | |
| <p> | |
| <label for="<?php echo $this->get_field_id( 'post_category' ); ?>">Category to Display:</label> | |
| <?php wp_dropdown_categories( array( | |
| 'name' => $this->get_field_name( 'post_category' ), | |
| 'show_option_all' => __( 'All Categories' ), | |
| 'hide_empty' => 0, | |
| 'hierarchical' => 0, | |
| 'selected' => $instance['post_category'], | |
| ) ); ?> | |
| </p> | |
| <?php | |
| } | |
| } | |
| add_action( 'widgets_init', create_function( '', "register_widget('PB_Recent_Post_Widget');" ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment