Here we will see Recent Post by category dynamically, recent post will change on every category according to category id.
So, we will create a widget here.
Simply paste this code in functions.php file of your theme,
Go to -> Appearance -> Widget, here you will find a widget, just drag and drop this widget in widget area.
Note : you can change or update display as you want by using css.
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | /*==============================*/ // Creating the widget class wpb_widget extends WP_Widget { function __construct() { parent::__construct( // Base ID of your widget 'wpb_widget', // Widget name will appear in UI __('CodyPaste Recent Post', 'cody_widget_recent'), // Widget description array( 'description' => __( 'Recent post by current category', 'cody_widget_recent' ), ) ); } // Creating widget front-end public function widget( $args, $instance ) { $title = apply_filters( 'widget_title', $instance['title'] ); // before and after widget arguments are defined by themes echo $args['before_widget']; if ( ! empty( $title ) ) echo $args['before_title'] . $title . $args['after_title']; // This is where you run the code and display the output echo $args['after_widget']; $category = get_category( get_query_var( 'cat' ) ); $category_id = $category->cat_ID; if($category_id==""){ $cats = wp_get_post_categories( get_the_ID(), array( 'fields' => 'ids' ) ); // get cat id of current post $category_id = $cats[0]; } $args = array( 'cat' => $category_id, 'posts_per_page' => 5, // Number of posts to display ); $recent_posts_query = new WP_Query($args); // The Loop if ($recent_posts_query->have_posts()) { while ($recent_posts_query->have_posts()) { $recent_posts_query->the_post(); $title = get_the_title(); $post_thumb = get_the_post_thumbnail_url( get_the_ID(), 'medium' ); $post_date = get_the_date( 'M d, Y' ); $author = get_the_author(); ?> <div style="width:100%; display: inline-block;"> <a href="<?php the_permalink(); ?>"> <div style="width:30%; float:left;"><img src="<?php echo $post_thumb; ?>" width="100%"></div> <div style="width:70%; float:left; padding:0 10px;"> <h4><?php echo $title; ?></h4> <p style="font-size:11px; color:#9f9f9f; text-transform:uppercase;">By: <?php echo $author; ?> / ON: <?php echo $post_date; ?></p> </div> </a> </div> <?php } }else { echo 'No posts found.'; } // Restore original Post Data wp_reset_postdata(); } // Widget Backend public function form( $instance ) { if ( isset( $instance[ 'title' ] ) ) { $title = $instance[ 'title' ]; } else { $title = __( 'Cody Paste Recent Post', 'cody_widget_recent' ); } // Widget admin form ?> <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> <?php } // Updating widget replacing old instances with new public function update( $new_instance, $old_instance ) { $instance = array(); $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; return $instance; } // Class wpb_widget ends here } // Register and load the widget function wpb_load_widget() { register_widget( 'wpb_widget' ); } add_action( 'widgets_init', 'wpb_load_widget' ); |