Instantly share code, notes, and snippets.
Created
September 16, 2020 16:52
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save t-kuni/2622ed1ada18b8b58ddace14fc1558e9 to your computer and use it in GitHub Desktop.
WordpressのSeriesプラグインのウィジェットをサムネイル画像が出力される様にカスタマイズした物
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 | |
class Custom_List_Related extends \WP_Widget { | |
/** | |
* Default arguments for the widget settings. | |
* | |
* @since 2.0.0 | |
* @access public | |
* @var array | |
*/ | |
public $defaults = array(); | |
/** | |
* Set up the widget's unique name, ID, class, description, and other options. | |
* | |
* @since 2.0.0 | |
* @access public | |
* @return void | |
*/ | |
public function __construct() { | |
// Set up the widget options. | |
$widget_options = array( | |
'classname' => 'custom-series-list-related', | |
'description' => esc_html__( "Displays a list of posts within the current post's series.", 'series' ) | |
); | |
// Create the widget. | |
parent::__construct( 'custom-series-list-related', __( 'Series - Custom List Related', 'series' ), $widget_options ); | |
// Set up defaults. | |
$this->defaults = array( | |
'title' => __( 'In Series', 'series' ), | |
'order' => 'ASC', | |
'orderby' => 'date', | |
'posts_per_page' => -1 | |
); | |
} | |
/** | |
* Outputs the widget based on the arguments input through the widget controls. | |
* | |
* @since 2.0.0 | |
* @access public | |
* @return void | |
*/ | |
public function widget( $sidebar, $instance ) { | |
if ( ! is_singular() ) | |
return; | |
// Set the $args for wp_get_archives() to the $instance array. | |
$args = wp_parse_args( $instance, $this->defaults ); | |
// Don't echo. | |
$args['echo'] = false; | |
// Get the series list. | |
$list = $this->list_related_posts( get_queried_object_id(), $args ); | |
// Only display if we have a series. | |
if ( empty( $list ) ) | |
return; | |
// Output the theme's widget wrapper. | |
echo $sidebar['before_widget']; | |
// If a title was input by the user, display it. | |
if ( ! empty( $instance['title'] ) ) | |
echo $sidebar['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $sidebar['after_title']; | |
// Output the list. | |
echo $list; | |
// Close the theme's widget wrapper. | |
echo $sidebar['after_widget']; | |
} | |
/** | |
* Displays the widget control options in the Widgets admin screen. | |
* | |
* @since 2.0.0 | |
* @access public | |
* @param array $instance | |
* @param void | |
*/ | |
public function form( $instance ) { | |
// Merge the user-selected arguments with the defaults. | |
$instance = wp_parse_args( (array) $instance, $this->defaults ); | |
// Orderby options. | |
$orderby = array( | |
'ID' => __( 'ID', 'series' ), | |
'author' => __( 'Author', 'series' ), | |
'none' => __( 'None', 'series' ), | |
'title' => __( 'Title', 'series' ), | |
'name' => __( 'Slug', 'series' ), | |
'date' => __( 'Date (Published)', 'series' ), | |
'modified' => __( 'Date (Modified)', 'series' ), | |
'random' => __( 'Random', 'series' ), | |
'comment_count' => __( 'Comment Count', 'series' ) | |
); | |
// Order options. | |
$order = array( | |
'ASC' => __( 'Ascending', 'series' ), | |
'DESC' => __( 'Descending', 'series' ) | |
); ?> | |
<p> | |
<label> | |
<?php esc_html_e( 'Title:', 'series' ); ?> | |
<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" /> | |
</label> | |
</p> | |
<p> | |
<label> | |
<?php esc_html_e( 'Order By:', 'series' ); ?> | |
<select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'orderby' ) ); ?>"> | |
<?php foreach ( $orderby as $option_value => $option_label ) : ?> | |
<option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['orderby'], $option_value ); ?>><?php echo esc_html( $option_label ); ?></option> | |
<?php endforeach; ?> | |
</select> | |
</label> | |
</p> | |
<p> | |
<label> | |
<?php esc_html_e( 'Order:', 'series' ); ?> | |
<select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'order' ) ); ?>"> | |
<?php foreach ( $order as $option_value => $option_label ) : ?> | |
<option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['order'], $option_value ); ?>><?php echo esc_html( $option_label ); ?></option> | |
<?php endforeach; ?> | |
</select> | |
</label> | |
</p> | |
<p> | |
<label> | |
<?php esc_html_e( 'Limit:', 'series' ); ?> | |
<input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'posts_per_page' ) ); ?>" value="<?php echo esc_attr( $instance['posts_per_page'] ); ?>" /> | |
</label> | |
</p> | |
<?php | |
} | |
/** | |
* Displays a list of posts by series. | |
* | |
* @since 2.0.0 | |
* @param array $args | |
* @return string | |
*/ | |
function list_posts( $args = array() ) { | |
if ( empty( $args['series'] ) ) | |
return; | |
$out = ''; | |
$post_id = 0; | |
if ( in_the_loop() ) | |
$post_id = get_the_ID(); | |
else if ( is_singular() ) | |
$post_id = get_queried_object_id(); | |
$defaults = array( | |
'series' => '', // term slug | |
'order' => 'ASC', | |
'orderby' => 'date', | |
'posts_per_page' => -1, | |
'echo' => true | |
); | |
$args = wp_parse_args( $args, $defaults ); | |
$query_args = array( | |
'order' => $args['order'], | |
'orderby' => $args['orderby'], | |
'posts_per_page' => $args['posts_per_page'], | |
'tax_query' => array( | |
array( | |
'taxonomy' => \Series\get_series_taxonomy(), | |
'field' => is_numeric( $args['series'] ) ? 'term_id' : 'slug', | |
'terms' => array( $args['series'] ) | |
) | |
) | |
); | |
$loop = new \WP_Query( $query_args ); | |
if ( $loop->have_posts() ) { | |
$out .= '<ul class="series-list">'; | |
while ( $loop->have_posts() ) { | |
$loop->the_post(); | |
$thumbnail = ""; | |
if ( has_post_thumbnail() ) { | |
$thumbnail = '<div>' . get_the_post_thumbnail() . '</div>'; | |
} | |
$title = get_the_title() ? the_title( '', '', false ) : get_the_ID(); | |
$out .= $post_id === get_the_ID() | |
? sprintf( '<li>%s%s</li>', $thumbnail, $title ) | |
: sprintf( '<li><a href="%s">%s%s</a></li>', esc_url( get_permalink() ), $thumbnail, $title ); | |
} | |
$out .= '</ul>'; | |
} | |
wp_reset_postdata(); | |
if ( false === $args['echo'] ) | |
return $out; | |
echo $out; | |
} | |
/** | |
* Displays a list of posts related to the current post. | |
* | |
* @since 2.0.0 | |
* @param int $post_id | |
* @param array $args | |
* @return string | |
*/ | |
function list_related_posts( $post_id = 0, $args = array() ) { | |
if ( ! $post_id ) | |
get_the_ID(); | |
if ( $post_id ) | |
$series = get_the_terms( $post_id, \Series\get_series_taxonomy() ); | |
if ( empty( $series ) ) | |
return; | |
$series = reset( $series ); | |
$args['series'] = $series->slug; | |
return $this->list_posts( $args ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment