Add a Like button to category/tag/taxonomy pages

like it button

In en earlier post I’ve showed you how we can add a like it button to a post/page without a plugin.

How to add a like button without a plugin

I’ve got a message asking how to also add this kind of button to a category/tag page. As we have stored the likes data as a post meta for posts/pages, similarly if we can also store it for a category/tag, this should be an easy task.

Fortunately WordPress 4.4 “Clifford” introduced term meta data which allows you to save meta values for terms in a similar way to post meta data.
Four new functions were introduced to handle create, read, update and delete operations for term meta data:

  • add_term_meta(): adds the meta data
  • update_term_meta(): updates existing meta data
  • delete_term_meta(): deletes meta data
  • get_term_meta(): retrieves the meta data

Under the hood, these functions use the same code that the corresponding functions for post meta data use as well.

And we are going to use them for our like feature for category/tag and custom taxonomy archive pages.

Follow below steps to add the button.

Step 1:

Add these codes to your functions.php

In Line 39, the tax-like-it.js is the file to contain the main javascript and we are going to create it in Step 2.

Step 2:

Create a new tax-like-it.js file and put below codes to it.

The look of the button depends on your theme’s styles, you can customize it on your needs.

Step 3:

Usage – Put this line of code to the category/tag or any other custom taxonomy archive page.
https://gist.github.com/shizhua/3023497bfe491d0ba789#file-usage-tax-like-php

Save

Author: Leo

My name is Leo who's behind this blog. I've been working with wordpress since 2010. I'll write about themes, plugins, tutorials and all about wordpress here. Hope you find it useful!

Leave a Reply

Your email address will not be published. Required fields are marked *