Skip to content

Instantly share code, notes, and snippets.

@nikola-wd
Created February 10, 2020 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikola-wd/c5d6bba722105dbe4203113195ce3b0d to your computer and use it in GitHub Desktop.
Save nikola-wd/c5d6bba722105dbe4203113195ce3b0d to your computer and use it in GitHub Desktop.
[WrdPress custom widget] custom recent posts widget #wordpress #php #custom #widget
<?php
// Added by Nikola on 2/10/2020 to fix the footer recent articles issues that's been probably happening due the ome server missconfiguration
// or some added plugins that caused the conflict, and caused the original one not to work properly
class CustomRecentPostsWidget extends WP_Widget {
function CustomRecentPostsWidget() {
parent::WP_Widget(false, $name = 'Custom Recent Posts');
}
function form($instance) {
$title = esc_attr($instance['title']);
$dis_posts = esc_attr($instance['dis_posts']);
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <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></p>
<p><label for="<?php echo $this->get_field_id('dis_posts'); ?>"><?php _e('Number of Posts Displayed:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('dis_posts'); ?>" name="<?php echo $this->get_field_name('dis_posts'); ?>" type="text" value="<?php echo $dis_posts; ?>" /></label></p>
<?php
}
function widget($args, $instance) {
extract( $args );
$title = apply_filters('widget_title', $instance['title']);
$dis_posts = $instance['dis_posts'];
?>
<?php echo $before_widget; ?>
<?php if ( $title )
echo $before_title . $title . $after_title; ?>
<ul>
<?php
global $post;
$args = array( 'numberposts' => $dis_posts);
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
<?php echo $after_widget; ?>
<?php
}
}
?>
<?php add_action('widgets_init', create_function('', 'return register_widget("CustomRecentPostsWidget");')); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment