Add the Categories/Tags Display Option to the MyThemeShop Related Posts Widget

If you’ve been using MyThemeShop themes, then you probably noticed that the widget you use to display related posts below the post content has the option to choose between tags or categories to be the deciding factor when the related posts appear. However, this option isn’t there in the widget you can use in the sidebars, and you’re forced to display posts based on tags.

Categories/Tags Display Option to the MyThemeShop Related Posts Widget

There should be an option that you can choose to display the posts based on tags or categories, but there isn’t. Fortunately, it’s not that hard to add it.

To do this, navigate to the theme files, wp-content/themes/ <mythemeshop theme name> /functions/ and look for widget-relatedposts.php, open it up with your favorite code/text editor.

The first thing we need to do is add the new variable that will hold this information. Go to line 38, where you should see a ton of variables, $excerpt_length like  and others. And add the following below the last one:

$related_posts_taxonomy = isset( $instance['related_posts_taxonomy'] ) ? $instance['related_posts_taxonomy'] : 'categories';

Then, we need to add the option to the widget settings panel. Where you’ll be able to choose between tags or categories display. For that, scroll down to line 55, and add the following below the “title_lenght” ‘s </p>

<p>
	<label for="<?php echo $this->get_field_id('related_posts_taxonomy'); ?>"><?php _e('Show Posts By:', 'socialme' ); ?></label>
	<select id="<?php echo $this->get_field_id('related_posts_taxonomy'); ?>" name="<?php echo $this->get_field_name('related_posts_taxonomy'); ?>">
		<option value="categories" <?php selected($related_posts_taxonomy, 'categories', true); ?>><?php _e('Categories', 'socialme' ); ?></option>
		<option value="tags" <?php selected($related_posts_taxonomy, 'tags', true); ?>><?php _e('Tags', 'socialme' ); ?></option>
	</select>
</p>

This will add a new dropdown option just below the title length field in the widget options.

However, before we can use this, we need to add the variable to the saving part of the widget. So the setting is saved once we click the save button.

To do this, go down to around line 110 where you’ll see a function called, update( $new_instance, $old_instance ) inside this function, you see a lot of new options. And just before the return $instance add the following:

$instance['related_posts_taxonomy'] = $new_instance['related_posts_taxonomy'];

Once done, the newly added option should be saved once you click the small blue save button.

Now that that’s done, we need to go to the function below that and retrieve the saved value from the database. So go to line 137, where you should see a few variables again. Below the last one, ( should be $excerpt_lenght ) add the following:

$related_posts_taxonomy = $instance['related_posts_taxonomy'];

With this, we stored the saved data into a variable called $related_posts_taxonomy so we can easily use it.

Move down a few more lines, around 170, where you should see something like this:

$posts = new WP_Query( 
	array(
		'category__in' => $cat_ids,
		'post__not_in' => array($thePostID),
		'posts_per_page' => $qty, 
		'orderby' => 'rand'
	));

And change it to the following:

if ($related_posts_taxonomy == 'categories') {

	$posts = new WP_Query( 
		array(
			'category__in' => $cat_ids,
			'post__not_in' => array($thePostID),
			'posts_per_page' => $qty, 
			'orderby' => 'rand'
		));

} else {

	$posts = new WP_Query( 
		array(
			'tag__in' => wp_get_post_tags(),
			'post__not_in' => array($thePostID),
			'posts_per_page' => $qty, 
			'orderby' => 'rand'
		));

};

This will check which one you set the option to, and display the related posts accordingly. If you selected tags, it’ll display based on tags. If categories, then based on categories.

Get more stuff like this

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.

Add Comment

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.