Wordpress
Jan14

Random post widget khusus themes kabar

Ktzagcplugin free agcplugin
Iklan

Random post widget khusus themes kabar, kegunaannya yaitu untuk menampilkan post secara random, baik berdasarkan category atau semua category, juga terdiri dari dua style. Silahkan bagi yang ingin mengcopy paste codenya.

Caranya:

1. Buat file baru bernama: random_posts_widget.php atau nanti bisa download filenya di akhir artikel.

2. Kopas atau copy paste kode berikut, taruh di file random_posts_widget.php

<?php
/* Kentooz Framework widget for random posts. */

class ktz_random_posts extends WP_Widget {
function ktz_random_posts() {
$widget_ops = array( 'classname' => 'ktz_random_post clearfix', 'description' => __( 'Random posts with category selection.',ktz_admin_textdomain ) );
$this->WP_Widget('ktz-random-posts', __( 'KTZ Random Posts',ktz_admin_textdomain ), $widget_ops);
$this->alt_option_name = 'ktz_random';
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') );
}
function widget($args, $instance) {
$cache = wp_cache_get('widget_random_posts', 'widget');
if ( !is_array($cache) )
$cache = array();
if ( isset($cache[$args['widget_id']]) ) {
echo $cache[$args['widget_id']];
return;
}
ob_start();
extract($args);
$style_latest = empty( $instance['style_latest'] ) ? 'list' : $instance['style_latest'];
$hide_thumb = isset($instance['hide_thumb']) ? $instance['hide_thumb'] : false;
$cats = empty( $instance['cats'] ) ? '' : $instance['cats'];
$title = apply_filters('widget_title', empty($instance['title']) ? __( 'Random Posts',ktz_admin_textdomain) : $instance['title']);
if ( !$number = (int) $instance['number'] )
$number = 10;
else if ( $number < 1 )
$number = 1;
else if ( $number > 15 )
$number = 15;
$ktzrandom = new WP_Query(array('showposts' => $number, 'nopaging' => 0, 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'cat' => $cats,'orderby' => 'rand',));
if ($ktzrandom->have_posts()) :
echo $before_widget;
if ( $title ) :
if ( $cats != '' ) { echo '<h4><span><a href="' . get_category_link( $cats ) . '" title="' . get_cat_name( $cats ) . '">' . get_cat_name( $cats ) . '</a><a href="' . get_category_feed_link ( $cats ) . '" title="' . get_cat_name( $cats ) . ' ' . __('RSS Feed',ktz_theme_textdomain) . '">&nbsp;</a></span></h4>'; }
else { echo '<h4><span>' . __('Random post', ktz_theme_textdomain) . '</span></h4>';}
endif;
global $post;
if ( $style_latest == "list" ) {
echo '<div><div>';
while ($ktzrandom -> have_posts()) : $ktzrandom -> the_post();
echo '<div>';
echo ktz_feature_70();
echo ktz_posted_title_h5();
echo ktz_posted_on();
echo '</div>';
echo '<hr />';
endwhile;
echo '</div></div>';
} else {
echo '<div>';
echo '<div>';
while ($ktzrandom -> have_posts()) : $ktzrandom -> the_post();
echo '<div>';
echo ktz_feature_140();
echo '<div>';
echo ktz_posted_title_h3();
echo '</div></div>';
endwhile;
echo '</div></div>';
}
wp_reset_query();
endif;
echo $after_widget;
$cache[$args['widget_id']] = ob_get_flush();
wp_cache_add('widget_random_posts', $cache, 'widget');
}

function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['style_latest'] = strip_tags( $new_instance['style_latest'] );
$instance['title'] = strip_tags($new_instance['title']);
$instance['number'] = (int) $new_instance['number'];
$instance['cats'] = strip_tags($new_instance['cats'] );
$instance['hide_thumb'] = isset($new_instance['hide_thumb']) ? true : false;
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions['ktz_random']) )
delete_option('ktz_random');
return $instance;
}

function flush_widget_cache() {
wp_cache_delete('widget_random_posts', 'widget');
}

function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'hide_thumb' => false,'cats' => '', 'style_latest' => 'list') );
$title = esc_attr( $instance['title'] );
$cats = esc_attr( $instance['cats'] );
if ( !isset($instance['number']) || !$number = (int) $instance['number'] )
$number = 5; ?>
<p><label for="<?php echo $this->get_field_id('title',ktz_admin_textdomain ); ?>"><?php _e( 'Title:',ktz_admin_textdomain ); ?></label>
<input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
<br/>
<small><?php _e( 'This title will not show in widget.',ktz_admin_textdomain ); ?></small>
</p>
<p><label for="<?php echo $this->get_field_id('number',ktz_admin_textdomain ); ?>"><?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" /><br />
<small><?php _e( '(at most 15)',ktz_admin_textdomain ); ?></small>
</p>
<p><label for="<?php echo $this->get_field_id('style_latest'); ?>"><?php _e( 'Style box:',ktz_admin_textdomain ); ?></label>
<select name="<?php echo $this->get_field_name('style_latest'); ?>" id="<?php echo $this->get_field_id('style_latest',ktz_admin_textdomain); ?>">
<option value="list"<?php selected( $instance['style_latest'], 'list' ); ?>><?php _e( 'list',ktz_admin_textdomain ); ?></option>
<option value="box"<?php selected( $instance['style_latest'], 'box' ); ?>><?php _e( 'box',ktz_admin_textdomain ); ?></option>
</select>
<br/>
<small><?php _e( 'Select style for latest widget.',ktz_admin_textdomain ); ?></small>
</p>
<p><label for="<?php echo $this->get_field_id('cats'); ?>"><?php _e( 'Fill with Category ID:',ktz_admin_textdomain); ?></label>
<select id="<?php echo $this->get_field_id('cats'); ?>" name="<?php echo $this->get_field_name('cats'); ?>">
<option value="" <?php selected( $instance['cats'], '' ); ?>><?php _e('All', ktz_admin_textdomain); ?></option>
<?php $blog_categories = get_categories( array('orderby' => 'id') ); foreach( $blog_categories as $category ): ?>
<option value="<?php echo $category->term_id; ?>" <?php selected( $instance['cats'], $category->term_id ); ?>><?php echo $category->name; ?></option>
<?php endforeach; ?>
</select>
<br />
<small><?php _e('Please select category for display in your widget', ktz_admin_textdomain); ?></small>
</p>
<p><label for="<?php echo $this->get_field_id( 'hide_thumb' ); ?>"><?php _e( 'Hide Thumbnails?',ktz_admin_textdomain); ?></label>
<input type="checkbox" <?php checked($instance['hide_thumb'], true) ?> id="<?php echo $this->get_field_id('hide_thumb'); ?>" name="<?php echo $this->get_field_name( 'hide_thumb' ); ?>" /><br />
<small><?php _e( 'If unchecked, it will show post thumbnails.',ktz_admin_textdomain); ?></small>
</p>
<?php }
}?>

3. Save kode tersebut taruh di folder kabar/includes/widget/

4. Buka file widget.php di folder kabar/includes/widget/

5. Setelah edit file tersebut, taruh kode berikut:

// Add random widget in kentooz framework
include_once(ktz_dir . '/includes/widget/random_posts_widget.php');
register_widget('ktz_random_posts');

setelah kode

function ktz_widget_init() {

6. Save file tersebut. Selesai, lalu tinggal diatur di appearance -> widget maka akan muncul widget baru bernama KTZ Random Post. Lihat contoh gambar berikut:

widget random

widget random

Silahkan download filenya, jangan lupa extract dulu karena file dalam bentuk zip. :)

Download Random Post Widget

Semoga bermanfaat dan bisa menerapkan di themes kabar masing masing. Salam. :)

 

Tags:
Iklan

Author: 

Freelance designer, tidak terpaku sama yang namanya waktu, hobby mengunjungi localhost setelah itu baru OL. Suka pusing kalau melihat kode kode yang aneh. :| Jangan lupa add ane di link social newtwork dibawah.

author

Related Posts "Random post widget khusus themes kabar"

Leave a reply "Random post widget khusus themes kabar"

Captcha: * Time limit is exhausted. Please reload CAPTCHA.