Skip to content

Instantly share code, notes, and snippets.

@webspace-jp
Last active January 3, 2016 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webspace-jp/8393494 to your computer and use it in GitHub Desktop.
Save webspace-jp/8393494 to your computer and use it in GitHub Desktop.
WordPress ランダムに投稿のリストを返します テンプレート埋め込み、ウィジェットに対応
<?php
/*
Plugin Name: [ws] random post
Plugin URI: http://webspace.jp/
Description: ランダムにエントリーを返す
Author: hukinotou
Version: 0.1
*/
include_once 'ws-widget-random-post.php';
function ws_the_random_post($args = '') {
echo ws_get_the_random_post($args);
}
function ws_get_the_random_post($args = '') {
$ws_random_post = new ws_random_post();
return $ws_random_post->execute($args);
}
class ws_random_post {
var $offset;
var $range;
function __construct() {
$this->ws_random_post_init();
}
function ws_random_post_init() {
}
function execute($args) {
global $post;
$args = apply_filters( 'ws_random_post_args', $args );
/*
* offset: 現在日時より指定日数以上過去の記事を対象とする
* range: offset指定日より、指定日数間の記事を対象とする(0: 無期限)
*/
$defaults = array(
'number' => 5,
'order' => 'DESC',
'orderby' => 'rand',
'offset' => 0,
'range' => 365,
'before' => '',
'after' => '',
'zero' => '',
'show_date' => false,
);
$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );
$this->offset = $offset;
$this->range = $range;
$optins = array(
array(
'relation' => 'OR',
),
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $number,
'order' => $order,
'orderby' => $orderby,
'ignore_sticky_posts' => 1,
);
if (is_single()) {
$optins['post__not_in'][] = $post->ID;
}
add_filter( 'posts_where', array($this, 'filter_where') );
$my_query = new WP_Query($optins);
remove_filter( 'posts_where', array($this, 'filter_where') );
$data = '';
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) {
$my_query->the_post();
$data .= '<li>';
$data .= sprintf('<a href="%1$s">%2$s</a>', get_permalink(), get_the_title());
if ($show_date) {
$data .= '<span class="post-date">' . get_the_date() . '</span>';
}
$data .= '</li>';
}
} else {
$data = $zero;
}
wp_reset_postdata();
return $this->build_html($before, $after, $data);
}
function build_html($before, $after, $content){
if (empty($content))
return '';
return $before . '<ul>' . $content . '</ul>' . $after;
}
function filter_where( $where = '' ) {
$totime = date_i18n("U") - ($this->offset * 24 * 60 * 60);
$where .= sprintf(" AND post_date <= '%s'", date('Y-m-d H:i:s', $totime));
if ($this->range > 0) {
$fromtime = $totime - ($this->range * 24 * 60 * 60);
$where .= sprintf(" AND post_date >= '%s'", date('Y-m-d H:i:s', $fromtime));
}
return $where;
}
}
<?php
class ws_widget_random_post{
function __construct() {
add_action( 'widgets_init', array($this, 'ws_widgets_init') );
}
function ws_widgets_init() {
register_widget("Widget_Random_Post");
}
}
new ws_widget_random_post();
class Widget_Random_Post extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'widget_random_post', 'description' => 'ランダムに投稿を表示する' );
parent::__construct('random_post', 'ランダムな投稿', $widget_ops);
}
function widget( $args, $instance ) {
extract($args);
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Random Posts' );
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 10;
if ( ! $number )
$number = 10;
$show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
$offset = ( ! empty( $instance['offset'] ) ) ? absint( $instance['offset'] ) : 0;
if ( ! $offset )
$offset = 0;
$range = ( ! empty( $instance['range'] ) ) ? absint( $instance['range'] ) : 365;
if ( ! $range )
$range = 365;
$args2 = array(
'number' => $number,
'offset' => $offset,
'range' => $range,
'show_date' => $show_date,
);
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title;
ws_the_random_post($args2);
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['number'] = (int) $new_instance['number'];
$instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
$instance['offset'] = (int) $new_instance['offset'];
$instance['range'] = (int) $new_instance['range'];
return $instance;
}
function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
$show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
$offset = isset( $instance['offset'] ) ? absint( $instance['offset'] ) : 0;
$range = isset( $instance['range'] ) ? absint( $instance['range'] ) : 365;
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" /></p>
<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
<p><input class="checkbox" type="checkbox" <?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?' ); ?></label></p>
<p><label for="<?php echo $this->get_field_id( 'offset' ); ?>">投稿を取得しない期間(日数):</label>
<input id="<?php echo $this->get_field_id( 'offset' ); ?>" name="<?php echo $this->get_field_name( 'offset' ); ?>" type="text" value="<?php echo $offset; ?>" size="3" /><br />
<small>現在日時より指定日数以上過去の記事を取得対象とします。</small></p>
<p><label for="<?php echo $this->get_field_id( 'range' ); ?>">投稿を取得する期間(日数):</label>
<input id="<?php echo $this->get_field_id( 'range' ); ?>" name="<?php echo $this->get_field_name( 'range' ); ?>" type="text" value="<?php echo $range; ?>" size="3" /><br />
<small>投稿を取得しない期間以降、指定日数間の記事を対象とします。<br />(0を設定すると無期限となります)</small></p>
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment