php 小部件存档档案
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 小部件存档档案相关的知识,希望对你有一定的参考价值。
class WP_Widget_Archives_Custom extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'widget_archive', 'description' => __( 'A monthly archive of your site’s posts') );
parent::__construct('archives', __('Archives'), $widget_ops);
}
function widget( $args, $instance ) {
extract($args);
$c = ! empty( $instance['count'] ) ? '1' : '0';
$d = ! empty( $instance['dropdown'] ) ? '1' : '0';
$title = apply_filters('widget_title', empty($instance['title']) ? __('Archives') : $instance['title'], $instance, $this->id_base);
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title;
if ( $d ) {
?>
<select name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> <option value=""><?php echo esc_attr(__('Select Month')); ?></option> <?php wp_get_archives(apply_filters('widget_archives_dropdown_args', array('type' => 'monthly', 'format' => 'option', 'show_post_count' => $c))); ?> </select>
<?php
} else {
?>
<ul>
<?php wp_get_archives_custom(apply_filters('widget_archives_args', array('type' => 'yearly', 'show_post_count' => $c))); ?>
</ul>
<?php
}
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$new_instance = wp_parse_args( (array) $new_instance, array( 'title' => '', 'count' => 0, 'dropdown' => '') );
$instance['title'] = strip_tags($new_instance['title']);
$instance['count'] = $new_instance['count'] ? 1 : 0;
$instance['dropdown'] = $new_instance['dropdown'] ? 1 : 0;
return $instance;
}
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'count' => 0, 'dropdown' => '') );
$title = strip_tags($instance['title']);
$count = $instance['count'] ? 'checked="checked"' : '';
$dropdown = $instance['dropdown'] ? 'checked="checked"' : '';
?>
<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 esc_attr($title); ?>" /></p>
<p>
<input class="checkbox" type="checkbox" <?php echo $dropdown; ?> id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>" /> <label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e('Display as dropdown'); ?></label>
<br/>
<input class="checkbox" type="checkbox" <?php echo $count; ?> id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" /> <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Show post counts'); ?></label>
</p>
<?php
}
}
add_action('widgets_init', create_function('', 'return register_widget("WP_Widget_Archives_Custom");'));
<?php
$rss = fetch_rss('http://rss.weather.com/weather/rss/local/UYXX0007?cm_ven=LWO&cm_cat=rss&par=LWO_rss');
$maxitems = 1;
$items = array_slice($rss->items, 0, $maxitems);
?>
<div class="top-box">
<?php if (empty($items)): ?>
No items
<?php else:
foreach ( $items as $item ):
?>
<strong class="title"><?php echo $item['title']; ?></strong>
<?php
$weatherimg = substr($item['summary'], 0, strripos($item['summary'],'/>'));
echo $weatherimg.' width="35" height="35" />';
?>
<div class="cell">
<?php
$celsium = '';
preg_match("/(\d+?\s\°\;?\s?\w)/", $item['summary'], $celsium);
if(!empty($celsium))
{
echo '<strong class="temp">'.str_replace(' ','',current($celsium)).'</strong>';
}
$desc = '';
$start = strripos($item['summary'],'/>')+2;
$end = strripos($item['summary'],',');
$length = $end - $start;
$desc = substr($item['summary'], $start, $length);
if(!empty($desc))
{
echo '<em>'.$desc.'</em>';
}
?>
</div>
<?php
endforeach;
endif;
?>
</div>
function wp_get_archives_custom($args = '') {
global $wpdb, $wp_locale;
$defaults = array(
'type' => 'monthly', 'limit' => '',
'format' => 'html', 'before' => '',
'after' => '', 'show_post_count' => false,
'echo' => 1, 'order' => 'DESC',
);
$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );
if ( '' == $type )
$type = 'monthly';
if ( '' != $limit ) {
$limit = absint($limit);
$limit = ' LIMIT '.$limit;
}
$order = strtoupper( $order );
if ( $order !== 'ASC' )
$order = 'DESC';
// this is what will separate dates on weekly archive links
$archive_week_separator = '–';
// over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride
$archive_date_format_over_ride = 0;
// options for daily archive (only if you over-ride the general date format)
$archive_day_date_format = 'Y/m/d';
// options for weekly archive (only if you over-ride the general date format)
$archive_week_start_date_format = 'Y/m/d';
$archive_week_end_date_format = 'Y/m/d';
if ( !$archive_date_format_over_ride ) {
$archive_day_date_format = get_option('date_format');
$archive_week_start_date_format = get_option('date_format');
$archive_week_end_date_format = get_option('date_format');
}
//filters
$where = apply_filters( 'getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );
$join = apply_filters( 'getarchives_join', '', $r );
$output = '';
if ( 'monthly' == $type ) {
$query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date $order $limit";
$key = md5($query);
$cache = wp_cache_get( 'wp_get_archives' , 'general');
if ( !isset( $cache[ $key ] ) ) {
$arcresults = $wpdb->get_results($query);
$cache[ $key ] = $arcresults;
wp_cache_set( 'wp_get_archives', $cache, 'general' );
} else {
$arcresults = $cache[ $key ];
}
if ( $arcresults ) {
$afterafter = $after;
$counter = 0;
foreach ( (array) $arcresults as $arcresult ) { ++$counter;
$url = get_month_link( $arcresult->year, $arcresult->month );
/* translators: 1: month name, 2: 4-digit year */
$text = sprintf(__('%1$s'), $wp_locale->get_month($arcresult->month));
if ( $show_post_count )
$after = ' ('.$arcresult->posts.')' . $afterafter;
$output .= get_archives_link($url, $text, $format, $before, $after);
}
}
} elseif ('yearly' == $type) {
$query = "SELECT YEAR(post_date) AS `year`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date) ORDER BY post_date $order $limit";
$key = md5($query);
$cache = wp_cache_get( 'wp_get_archives' , 'general');
if ( !isset( $cache[ $key ] ) ) {
$arcresults = $wpdb->get_results($query);
$cache[ $key ] = $arcresults;
wp_cache_set( 'wp_get_archives', $cache, 'general' );
} else {
$arcresults = $cache[ $key ];
}
$query2 = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date $order $limit";
$key2 = md5($query2);
$cache2 = wp_cache_get( 'wp_get_archives' , 'general');
if ( !isset( $cache2[ $key2 ] ) ) {
$arcresults2 = $wpdb->get_results($query2);
$cache2[ $key2 ] = $arcresults2;
wp_cache_set( 'wp_get_archives', $cache2, 'general' );
} else {
$arcresults2 = $cache2[ $key2 ];
}
if ($arcresults) {
$afterafter = $after;
foreach ( (array) $arcresults as $arcresult) {
$url = get_year_link($arcresult->year);
$text = sprintf('%d', $arcresult->year);
if ($show_post_count)
$after = ' ('.$arcresult->posts.')' . $afterafter;
$output .= '<li><a class="alt-item" href="'.get_year_link($arcresult->year).'">'.$arcresult->year.'</a></li>';
if ( $arcresults2 ) {
$afterafter2 = $after2;
foreach ( (array) $arcresults2 as $arcresult2 ) {
if($arcresult2->year == $arcresult->year)
{
$output .= '<li><a href="'.get_month_link( $arcresult->year, $arcresult2->month ).'">'.sprintf(__('%1$s'), $wp_locale->get_month($arcresult2->month)).'</a></li>';
}
}
}
}
}
} elseif ( 'daily' == $type ) {
$query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, DAYOFMONTH(post_date) AS `dayofmonth`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date $order $limit";
$key = md5($query);
$cache = wp_cache_get( 'wp_get_archives' , 'general');
if ( !isset( $cache[ $key ] ) ) {
$arcresults = $wpdb->get_results($query);
$cache[ $key ] = $arcresults;
wp_cache_set( 'wp_get_archives', $cache, 'general' );
} else {
$arcresults = $cache[ $key ];
}
if ( $arcresults ) {
$afterafter = $after;
foreach ( (array) $arcresults as $arcresult ) {
$url = get_day_link($arcresult->year, $arcresult->month, $arcresult->dayofmonth);
$date = sprintf('%1$d-%2$02d-%3$02d 00:00:00', $arcresult->year, $arcresult->month, $arcresult->dayofmonth);
$text = mysql2date($archive_day_date_format, $date);
if ($show_post_count)
$after = ' ('.$arcresult->posts.')'.$afterafter;
$output .= get_archives_link($url, $text, $format, $before, $after);
}
}
} elseif ( 'weekly' == $type ) {
$week = _wp_mysql_week( '`post_date`' );
$query = "SELECT DISTINCT $week AS `week`, YEAR( `post_date` ) AS `yr`, DATE_FORMAT( `post_date`, '%Y-%m-%d' ) AS `yyyymmdd`, count( `ID` ) AS `posts` FROM `$wpdb->posts` $join $where GROUP BY $week, YEAR( `post_date` ) ORDER BY `post_date` $order $limit";
$key = md5($query);
$cache = wp_cache_get( 'wp_get_archives' , 'general');
if ( !isset( $cache[ $key ] ) ) {
$arcresults = $wpdb->get_results($query);
$cache[ $key ] = $arcresults;
wp_cache_set( 'wp_get_archives', $cache, 'general' );
} else {
$arcresults = $cache[ $key ];
}
$arc_w_last = '';
$afterafter = $after;
if ( $arcresults ) {
foreach ( (array) $arcresults as $arcresult ) {
if ( $arcresult->week != $arc_w_last ) {
$arc_year = $arcresult->yr;
$arc_w_last = $arcresult->week;
$arc_week = get_weekstartend($arcresult->yyyymmdd, get_option('start_of_week'));
$arc_week_start = date_i18n($archive_week_start_date_format, $arc_week['start']);
$arc_week_end = date_i18n($archive_week_end_date_format, $arc_week['end']);
$url = sprintf('%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', home_url(), '', '?', '=', $arc_year, '&', '=', $arcresult->week);
$text = $arc_week_start . $archive_week_separator . $arc_week_end;
if ($show_post_count)
$after = ' ('.$arcresult->posts.')'.$afterafter;
$output .= get_archives_link($url, $text, $format, $before, $after);
}
}
}
} elseif ( ( 'postbypost' == $type ) || ('alpha' == $type) ) {
$orderby = ('alpha' == $type) ? 'post_title ASC ' : 'post_date DESC ';
$query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit";
$key = md5($query);
$cache = wp_cache_get( 'wp_get_archives' , 'general');
if ( !isset( $cache[ $key ] ) ) {
$arcresults = $wpdb->get_results($query);
$cache[ $key ] = $arcresults;
wp_cache_set( 'wp_get_archives', $cache, 'general' );
} else {
$arcresults = $cache[ $key ];
}
if ( $arcresults ) {
foreach ( (array) $arcresults as $arcresult ) {
if ( $arcresult->post_date != '0000-00-00 00:00:00' ) {
$url = get_permalink( $arcresult );
if ( $arcresult->post_title )
$text = strip_tags( apply_filters( 'the_title', $arcresult->post_title, $arcresult->ID ) );
else
$text = $arcresult->ID;
$output .= get_archives_link($url, $text, $format, $before, $after);
}
}
}
}
if ( $echo )
echo $output;
else
return $output;
}
以上是关于php 小部件存档档案的主要内容,如果未能解决你的问题,请参考以下文章