Sparkling Popular Posts Widget To Recent Posts

Hello, thank you for this great theme!

I’d like to have both Sparkling Popular Posts Widget and Sparkling Recent Posts Widget which is not exist I know. Here’s the question: How can I duplicate Sparkling Popular Posts Widget and mod the duplicated one so that it works with Recents posts?

I already tried Recent Post Extended and managed it to look exactly like Sparkling Popular Posts Widget but it makes lots of troubles. It didn’t designed like Sparkling one and for instance, when a title gets 3 lines the last word gets under the thumb. And the spacing between items behaves oddly. Besides, all of the customizations work on desktop mode only, whenever you test it with a tablet or a gadget with smaller screen all of the layout of Recent Post Extended widget gets crazy. So I thought, instead of spending lots of times for trying to fix that widget makes, I could just duplicate a widget designed for this theme and change a simple code to make it bring not popular but recent posts.

Thanks.

Ps: Since I’m developing my site locally I have no link to provide.

You are not going to believe how easy it is to create recent post widget based on popular post widget.

You need to change a single line in Popular post fidget file from

'orderby' => 'comment_count',

to

'orderby' => 'date',

Of course you have to create a separate file, call for it via functions.php file just like with current widgets but since you already have an example with popular post widget, I don’t think it is going to be a big problem.

Let me know if this helps.

Thank you!

I did the change and saved it as widget-recent-post.php. Now I should call it in functions.php with require_once(get_template_directory() . ‘/inc/widgets/widget-recent-posts.php’); but I’m using child-theme, so just adding this line above to the child’s function.php do the trick?

And I need to change widget’s name on WP too, in order to distinguish it from popular one. So, what else I should change in widget-recent-post.php?

Guess these changes will do the trick, Popular to Recent:

$widget_ops = array('classname' => 'sparkling-popular-posts','description' => __( "Sparkling Recent Posts Widget", 'sparkling') );
$this->WP_Widget('sparkling_popular_posts', __('Sparkling Recent Posts Widget','sparkling'), $widget_ops);

Thanks again!

Hello archangeliques,

Please follow the following procedure:

  1. Go to wp-content\themes\sparkling\inc\widgets

a) Open file “widget-popular-posts.php” for editing and save it as “widget-recent-posts.php” in the same directory.

b) Open the newly created “widget-recent-posts.php” file for editing and replace the term “popular” with “recent”, save the file and exit.

  1. Go to wp-content\themes\sparkling

a) Open the file “functions.php” for editing. Paste the code register_widget( ‘sparkling_recent_posts’ ); in line 158 and paste require_once(get_template_directory() . ‘/inc/widgets/widget-recent-posts.php’); in “Theme Widgets” Area just below that section, save the file and exit.

  1. Go to WordPress Dashboard> Appearance> Widgets. Drag “Sparkling Recent Posts Widget” into the sidebar Area. Rename the title to “Recent Posts” if required.

Since you’re using a child theme for Sparkling, so you need to edit/add the code to the respective files in Child Theme as described above.

Go to your website and refresh it, changes should be visible now. If any further assistance is required please let us know. We would be glad to assist you further.

I did the things that both of you said but the results are below.

Now, what I have? 2 Scenarios.

Scenario 1: in widget-popular-posts.php I’ve only changed

'orderby' => 'comment_count', with 'orderby' => 'date',

and saved as widget-recent-posts.php and uploaded it to inc/widgets.

Then, I’ve added

require_once(get_template_directory() . '/inc/widgets/widget-recent-posts.php');

into functions.php in my child theme. I got the error below:

Fatal error: Cannot redeclare class sparkling_popular_posts in /test/test-site-03/wp-content/themes/sparkling/inc/widgets/widget-popular-posts.php on line 117

Scenario 2: in widget-popular-posts.php I’ve changed every popular with recent

and saved as widget-recent-posts.php and uploaded it to inc/widgets.

Then, I’ve added

       register_widget( 'sparkling_recent_posts' );
	require_once(get_template_directory() . '/inc/widgets/widget-recent-posts.php');

into functions.php in my child theme. I got the error below:

Fatal error: Class ‘sparkling_recent_posts’ not found in /test/test-site-03/wp-includes/widgets.php on line 413

my function.php in child theme includes:

<?php
	wp_enqueue_style(  'sparkling-fonts', '//fonts.googleapis.com/css?family=Open+Sans:400italic,400,600,700|Roboto+Slab:400,300,700&subset=latin,latin-ext');

	register_widget( 'sparkling_recent_posts' );
	require_once(get_template_directory() . '/inc/widgets/widget-recent-posts.php');
?>

I’ve added wp_enqueue_style in order to have latin and latin-ext support of Open Sans font. It’s working fine with that. Is it the proper way to add latin support btw?

I haven’t touched the original functions.php file in Sparkling theme but edited the one in the child theme. I think this is the way it should be, however, what am I doing wrong?

Thanks!

Ok, I see. I did forget to mention that you also need to replace

sparkling_popular_posts

with

sparkling_recent_posts

Everywhere in the newly created widget-recent-posts.php

Right now WordPress is calling for that function but it is not yet defined as you still have the old one.

Let me know if this helps.

I already did that in Scenario 2 of my previous post and got the message below:

Fatal error: Class ‘sparkling_recent_posts’ not found in /test/test-site-03/wp-includes/widgets.php on line 413

Any other suggestion?

Thx.

Hello archangeliques,

There is no need to change anything in functions.php file inside the sparkling-child folder. Your code inside the functions.php should do just fine.

Please try moving the “widget-recent-posts.php” from child theme directory sparkling-child/inc/widgets to the parent theme directory sparkling/inc/widgets and it should work fine.

Let us know if any further help is required.

Hello Amit,

Thanks for the answer.

I haven’t changed anything in the original functions.php. I added those codes into the child’s functions.php.

Let me clarify things a bit, the code below is the code in my functions.php that is in child’s folder.

<?php
	wp_enqueue_style(  'sparkling-fonts', '//fonts.googleapis.com/css?family=Open+Sans:400italic,400,600,700|Roboto+Slab:400,300,700&subset=latin,latin-ext');

	register_widget( 'sparkling_recent_posts' );
	require_once(get_template_directory() . '/inc/widgets/widget-recent-posts.php');
?>

I have no folder called “inc” in child theme either. “widget-recent-posts.php” is already in where it is supposed to be, in “sparkling/inc/widgets”.

Like I said before, I already did what both of you said and got the errors respectively.

This error belongs to Aigars suggestion (replace only “comment_count” with “date” in sparkling_recent_posts.php and add only “require_once” function into the functions.php that is in child’s theme):
Fatal error: Cannot redeclare class sparkling_popular_posts in /test/test-site-03/wp-content/themes/sparkling/inc/widgets/widget-popular-posts.php on line 117

And this one belongs to yours (replace every “popular” with “recent” in sparkling_recent_posts.php and add “register_widget” and "require_once: functions into the functions.php of child’s theme):
Fatal error: Class ‘sparkling_recent_posts’ not found in /test/test-site-03/wp-includes/widgets.php on line 413

Hope it’s clear this time. I’m not a code guy but I think I didn’t make any mistakes applying your suggestions, respectively. And still hoping to fix these fatal errors.

Thanks again!

I see that we are mixing everything together here :slight_smile:

I went ahead and create a Child Theme which has recent post widget includes, proper way to register this function and it has no need to make any modifications in parent theme and new widget is loaded directly from Child Theme.

You can download it here:
https://colorlib.com/wp/wp-content/uploads/sites/2/sparkling-child1.zip

I see that you had some other function in Child Theme but I didn’t add those I hope that you don’t mind combining your old Child theme with this one.

You are the man Aigars! Thank you!

As a non dev, I never tought adding the whole function into the child’s functions.php.

I have a few more questions if you don’t mind.
01. I’ve added the first line on my functions.php in order to get latin character support. Is it correct way to do it?

wp_enqueue_style( 'sparkling-fonts', '//fonts.googleapis.com/css?family=Open+Sans:400italic,400,600,700|Roboto+Slab:400,300,700&subset=latin,latin-ext');

02. If I wanted to show 10 posts with Both of the Sparkling Popular and the new Recent Widget I need to set it 9 as it adds 1 more for some reason.

03. I don’t think my Sparkling Popular Posts Widget working btw. I checked the stats and the posts it shows are not the most clicked ones. Besides, it always shows my the very first post at the top, then lists other posts by date. Right now except the first post it has the identical post list with Recent posts widget. Any idea?

Thanks!

  1. Your code is going to throw a error. Here try to use this instead.

add_action( ‘wp_enqueue_scripts’, ‘child_add_scripts’ );

/**
 * Register Google Fonts via Sparkling Child Theme
 */
function child_add_scripts() {
    wp_enqueue_style( 'sparkling-fonts', '//fonts.googleapis.com/css?family=Open+Sans:400italic,400,600,700|Roboto+Slab:400,300,700&subset=latin,latin-ext');
}

I might include this in Parent Theme in upcoming updates.

  1. I will look into this one and maybe could fix this within the next update as well if this is a global problem.

  2. Popularity is based on number of comments. WordPress itself doesn’t track number of hits (Jetpack, Analytics etc does it), so I have no data to use for widget. There are certain hacks that can be done by adding custom counters inside all template files but this solution won’t be included in theme.