对WordPress小工具进行对象缓存

作者: 站长 上传时间: 浏览: N/A 下载: N/A 格式: N/A 评分: N/A

缓存“ ak_events”的自定义帖子类型


class show_ak_events_Widget extends WP_Widget {
function show_ak_events_Widget() {
/* Widget settings. */
$widget_ops = array( 'classname' => 'ak-events', 'description' => 'Shows events in a table' );
/* Widget control settings. */
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'ak-events' );
/* Create the widget. */
$this->WP_Widget( 'ak-events', 'Show Events', $widget_ops, $control_ops );
}

function widget( $args, $instance ) {
extract( $args );
// get cache if it exists
// $widget_id comes from the widget $args->widget_id and is the widgets unique ID
$output = get_transient('events'.$widget_id);

// if no $output do stuff inside this if statement
if ( $output === false ) {
// set the title variable
$title = apply_filters('widget_title', $instance['title'] );

// standard opening of widget
$output = $before_widget;

// if a title exists add it to the top of the widget
$output .= ( !empty( $title ) )? $before_title . $title . $after_title : "" ;

// Create query arguments for WP_Query to use
$widgetargs = array( 'posts_per_page'=>'-1',
'post_type'=>'ak_events',
'post_status'=>'publish'
);

// WP_Query sets up a loop query
$query = new WP_Query( $widgetargs );

// create the opening table and top row
$output .= "

";

// If the WP_Query has results send them through the loop
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
$output .= "

";
endwhile;endif;

// close the table
$output .= "

Event Name Information
" . get_the_title() . " " . get_the_excerpt() . "

";

// close widget properly
$output .= $after_widget;

// save $output as a transient and set it to be 60 seconds * 5 = 5 minutes.
//
set_transient( 'events'.$widget_id, $output, 60*5 );
}

echo $output;

}

function update( $new_instance, $old_instance ) {
// save form data
$instance = $old_instance;
$instance['title'] = $new_instance['title'];

// delete the transient so the new title setting is used
delete_transient('events'.$this->id);
return $instance;
}

function form( $instance ) {
$defaults = array(
'title'=>''
);
$instance = wp_parse_args( (array) $instance, $defaults ); ?>


name="get_field_name( 'title' ); ?>"
value="" style="width:95%" />

Leave a Comment