缓存“ ak_events”的自定义帖子类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | 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 .= "<table><tr><th>Event Name</th><th>Information</th></tr>"; // If the WP_Query has results send them through the loop if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); $output .= "<tr><td>" . get_the_title() . "</td><td> " . get_the_excerpt() . " </td></tr>"; endwhile;endif; // close the table $output .= "</table>"; // 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 ); ?> <p> <label for="<?php echo $this->get_field_id( 'title' ); ?>"> <?php _e('Title:','proving-ground'); ?> </label> <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:95%" /> </p> <?php } } |
原文链接:https://xiaohost.com/3643.html,转载请注明出处。
评论0