How can I show datetimestamp (Month DD, YYYY HH:MM) like January 10, 2016 10:30PM on my website(dailyworld.in) under "AS IT COMES" which is a RECENT POST widget??
Hi,
If you want to resolve this issue, you can add the following code to the functions.php
file.
function wpse97413_register_custom_widgets() {
register_widget( 'wpse97411_Widget_Recent_Posts' );
}
add_action( 'widgets_init', 'wpse97413_register_custom_widgets' );
/**
* Recent_Posts widget class
*
* @since 2.8.0
*/
class wpse97411_Widget_Recent_Posts extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your site") );
parent::__construct('recent-posts', __('Customize Recent Posts'), $widget_ops);
$this->alt_option_name = 'widget_recent_entries';
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_recent_posts', 'widget');
if ( !is_array($cache) )
$cache = array();
if ( ! isset( $args['widget_id'] ) )
$args['widget_id'] = $this->id;
if ( isset( $cache[ $args['widget_id'] ] ) ) {
echo $cache[ $args['widget_id'] ];
return;
}
ob_start();
extract($args);
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent 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;
$r = new WP_Query( apply_filters( 'widget_posts_args', array( 'posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true ) ) );
if ($r->have_posts()) :
?>
<?php echo $before_widget; ?>
<?php if ( $title ) echo $before_title . $title . $after_title; ?>
<?php while ( $r->have_posts() ) : $r->the_post(); ?>
-
" title="<?php echo esc_attr( get_the_title() ? get_the_title() : get_the_ID() ); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?>
<?php if ( $show_date ) : ?>
<span class="post-date"><?php echo get_the_date('F j, Y g:i a'); ?></span>
<?php endif; ?>
<?php endwhile; ?>
<?php echo $after_widget; ?>
<?php
// Reset the global $the_post as this query will have stomped on it
wp_reset_postdata();
endif;
$cache[$args['widget_id']] = ob_get_flush();
wp_cache_set('widget_recent_posts', $cache, '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'] = (bool) $new_instance['show_date'];
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions['widget_recent_entries']) )
delete_option('widget_recent_entries');
return $instance;
}
function flush_widget_cache() {
wp_cache_delete('widget_recent_posts', 'widget');
}
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;
?>
<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; ?>" />
<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" />
<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>
<?php
}
}
NOTE: This is default plugin of the WordPress.
Hope this helps !
After applying above code to function.php, I started getting Post as below:
" title="Balotelli used to urinate on our boots: Ex-teammate">Balotelli used to urinate on our boots: Ex-teammate January 11, 2016 1:44 pm " title="At minus 15 degrees, Kargil coldest town in Kashmir">At minus 15 degrees, Kargil coldest town in Kashmir January 11, 2016 1:44 pm " title="Ghulam Ali plans Mumbai visit, all eyes on Shiv Sena">Ghulam Ali plans Mumbai visit, all eyes on Shiv Sena January 11, 2016 1:43 pm " title="Unable to use Indian vocals due to language barrier: Swedish DJ Axwell">Unable to use Indian vocals due to language barrier: Swedish DJ Axwell January 11, 2016 1:36 pm " title="Juventus climb to second spot in Serie A">Juventus climb to second spot in Serie A January 11, 2016 1:30 pm " title="Delhi Deputy CM watches film on commercialisation of education">Delhi Deputy CM watches film on commercialisation of education January 11, 2016 1:30 pm " title="Human activity leading Earth into a new geological epoch">Human activity leading Earth into a new geological epoch January 11, 2016 1:30 pm " title="ADB to provide $625 mn loan for Vizag-Chennai corridor">ADB to provide $625 mn loan for Vizag-Chennai corridor January 11, 2016 1:30 pm " title="Madhu Koda gets Rs.20 lakh extortion call">Madhu Koda gets Rs.20 lakh extortion call January 11, 2016 1:24 pm " title="Coconut tree is no tree? Goa government is nuts, says opposition">Coconut tree is no tree? Goa government is nuts, says opposition January 11, 2016 1:24 pm " title="Hrithik gifts himself Rolls Royce on birthday?">Hrithik gifts himself Rolls Royce on birthday? January 11, 2016 1:24 pm " title="Effort to revive Afghanistan peace talks begins in Pakistan">Effort to revive Afghanistan peace talks begins in Pakistan January 11, 2016 1:11 pm " title="Drug lord Guzman closer to US trial as Mexico starts extradition
Please send me the correct code for the same. Thanks!
I think you have resolved this issue.
Please login or Register to submit your answer