How to override the 'Shufflehound Recent Posts' in a child-theme ?

Home Forums Jevelin Theme How to override the 'Shufflehound Recent Posts' in a child-theme ?

Home Forums Jevelin Theme How to override the 'Shufflehound Recent Posts' in a child-theme ?

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
  • I’d like to know if I can override a widget by copying it in my child-theme and just change the code as I need ?

     

    In this exemple, I’d like to use your ‘Shufflehound Recent Posts’ to show only posts of a selected category.

     

    If I copy the widget in my child theme and modify the line :

     

    $posts = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => $limit ) );

     

    with this :

    $posts = new WP_Query( array( 'category_name' => 'my-category', 'post_type' => 'post', 'posts_per_page' => $limit ) );

     

    Nothing change…

    It works if I change the widget code in the Parent Theme. (not the best way for update)

     

    Thanks and once again great job, your theme is awesome.

    Hi @urbansurfers,

     

    I hope you are well today and thank you for your question.

     

    You can override the ‘Shufflehound Recent Posts’ widget in a child-theme by adding following widget class Widget_Recent_Posts in the functions.php file of your child theme.

     

    class Widget_Recent_Posts extends WP_Widget {
    
    		function __construct() {
    			$widget_ops = array( 'description' => esc_html__( 'Recent Postss', 'jevelin' ) );
    
    			parent::__construct( false, esc_html__( 'Shufflehound Recent Posts', 'jevelin' ), $widget_ops );
    		}
    
    		function widget( $args, $instance ) {
    			extract( $args );
    			$params = array();
    
    			foreach ( $instance as $key => $value ) {
    				$params[ $key ] = $value;
    			}
    
    			$title = $before_title . $params['widget-title'] . $after_title;
    			unset( $params['widget-title'] );
    
    			$filepath = get_template_directory() . '/inc/widgets/recent-posts/views/widget.php';
    
    			$instance      = $params;
    			$before_widget = str_replace( 'class="', 'class="widget_social_links ', $before_widget );
    
    			if ( file_exists( $filepath ) ) {
    				include $filepath;
    			}
    		}
    
    		function update( $new_instance, $old_instance ) {
    			$instance = wp_parse_args( (array) $new_instance, $old_instance );
    
    			return $instance;
    		}
    
    		function form( $instance ) {
    
    			$titles = array(
    				'widget-title' => esc_html__( 'Recent Posts Title:', 'jevelin' ),
    				'items_per_page'     => esc_html__( 'Items Per Page:', 'jevelin' ),
    			);
    
    			$instance = wp_parse_args( (array) $instance, $titles );
    
    			foreach ( $instance as $key => $value ) {
    				?>
    				<p>
    					<label><?php echo esc_attr( $titles[ $key ] ); ?></label>
    					<input class="widefat widget_social_link widget_link_field"
    					       name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="text"
    					       value="<?php echo ( $instance[ $key ] === $titles[ $key ] ) ? '' : esc_attr( $instance[ $key ] ); ?>"/>
    				</p>
    			<?php
    			}
    		}
    	}

     

    Best regards,
    Shufflehound team

    Hi,

    thanks for this quick answer !

     

    The code you give me doesn’t work if I put the modified widget in my Child-Theme.

     

    I assume I have to change :

    $filepath = get_template_directory() . '/inc/widgets/recent-posts/views/widget.php';

     

    by this :
    $filepath = get_stylesheet_directory() . '/inc/widgets/recent-posts/views/widget.php';

    (change the widget path for the Child-Theme folder)

     

    Now its work like a charm…

    The other solution I found was to copy the entire widget in my Child-Theme, modify every files to declare a new Widget and adapt it as I need.

     

    Thanks again for your work and reactivity

     

    PS: just for your further update, I found a small error in the code of your ‘Shufflehound Recent Posts’ :

    $widget_ops = array( 'description' => esc_html__( 'Recent Postss', 'jevelin' ) );

     

    Postss with 2 s (line 7 of the class-widget-recent-posts.php) and the same in your javelin.pot

    The shared code is working fine for me by adding the widget class Widget_Recent_Posts code in the functions.php file of my child theme.

    This class is wrapped in class_exists condition as displayed below therefore this class is over writable just by copying it in the child theme file and without changing any theme files.

    if( !class_exists('Widget_Recent_Posts') ) :
    	class Widget_Recent_Posts extends WP_Widget {

    May be you are doing something wrong which is causing it not to work simply by overwriting widget class.

     

    PS: just for your further update, I found a small error in the code of your ‘Shufflehound Recent Posts’ :

    $widget_ops = array( 'description' => esc_html__( 'Recent Postss', 'jevelin' ) );

     

    Postss with 2 s (line 7 of the class-widget-recent-posts.php) and the same in your javelin.pot

    Thank you for notifying us about this.

     

    I could confirm the issue on my test site therefore notified the theme developer about this so that it will be fixed.

     

    The issue will be fixed ASAP.

     

    Your help here is really appreciated.

Viewing 4 posts - 1 through 4 (of 4 total)