In the development of WP template, how to add the like function to the articles on the wordpress website?, WordPress Template Development Tutorial

1 year ago (2023-12-05) Chief Editor
6 minutes
three hundred and fifty-seven
two

When we browse articles on some websites online, there will be a "like" button at the end of the article. If you think the content of the article is very good, you can click the "like" button to like the article. In the template theme development of wordpress website, can we also add this "like" function to our articles on wordpress website? The answer is yes. So, How can I add the "like" function to articles on WordPress? See how I achieved it.

Step 1: introduce jquery into the header. php file of the wordpress template.

In order to transfer data to the background when clicking the "Like" button, we need to use the AJAX processing function of jquery here. Therefore, we need to first import the jquery file in the header file of the wordpress template. Here is version 1.7.2 of jquery, and other versions can also be used. The codes are as follows:

<script src="<?php bloginfo("template_url"); ?>/js/jquery-1.7.2.min.js"></script>

Step 2: Add the "Like" button below the wordpress article content. The codes are as follows:

<div class="item single_praise">Like:<span class="dashicons before dashicons heart"></span><span class="praise_num"><? php$praise_num = get_post_meta($post->ID,post_praise)[0]; // Get likes echo $praise_num$ praise_num : 0;?></ span></div>

Step 3: Add the event handling code for clicking "Like".

We need to add an event to the "Like" button. As soon as we click the "Like" button, the corresponding event processing will pop up, and the ID number of the current article will be transferred to the single_praise.php file through AJAX. The codes are as follows:

<script type="text/javascript">$(function()) {var pid=<? Php echo $post ->ID;?>;//Article IDvar user=<? Php echo wp_get_current_user() ->user_login;?>;//The current login user name

$(".single_praise").click(function(){

$. ajax ({type: post, url:<? Php bloginfo ("template_url");?>/include/single_praise.php, data: {pid: pid, user: user}, success: function (e) {console. log (e) var num=e? E: 0; $(". raise_num"). html (num);//Write likes again

}})})})</script>

Step 4: add "like" data to the corresponding article in the wordpress database.

Create a single_praise.php file under the include directory in the wordpress template directory to receive the data transferred from AJAX, and then add or modify data (like data of articles) to the wordpress database. The codes are as follows:

<? phpdefine(BASE_PATH,str_replace( \\ , / , realpath(dirname(__FILE__)./../../../../)));// Get the root directory require (BASE_PATH./wp load. php)$ postid = esc_sql($_POST[pid]);$ user = esc_sql($_POST[user]);$ u_ip = $_SERVER["REMOTE_ADDR"]; If ($postid==0) {//If the article ID=0exit ("illegal operation");}//Add like data to the database $praise_num=get_post_meta ($postid, post_prise) [0]$ user_views = ! empty($praise_num) ? $ praise_num : 0; update_post_meta($postid,post_praise,$user_views+1); print_r($praise_num); exit;

Through the above five steps, we have added the like function to the article on the wordpress website. Every time we click, we will add+1 to the like data of the article. In this way, we have basically completed the operation. However, there is a fly in the ointment. If the same user continuously clicks the "like" button, he will keep increasing the number of likes, which is unfriendly Generally, we only allow one user to like it once in a day. We will introduce this function in the next chapter. Please wait and see you in the next chapter

This article is written by: Chief Editor Published on Software Development of Little Turkey , please indicate the source for reprinting: //hongchengtech.cn/blog/1843.html
Kuke_WP editor
author

Related recommendations

1 year ago (2024-02-20)

What technologies have been applied and developed in the field of new media, and the application of new technologies in media

In the field of new media, many technologies have been applied and developed. These include: cloud computing: cloud computing technology enables new media companies to develop and deploy applications more quickly, and can dynamically adjust resources according to needs. Big data: New media companies can use big data technology to analyze massive user data, understand user preferences, behavior habits and other information
nine hundred and seventy-one
one
1 year ago (2024-02-20)

WMS warehouse management system, promoting the transformation of warehousing from extensive to fine management, WMS warehouse management

Warehousing is extremely important for the manufacturing industry and is an important guarantee for the survival and development of manufacturing enterprises. However, with the expansion of manufacturing enterprises' business, the traditional warehouse management has been unable to respond to business changes quickly. The extensive management mode not only makes the warehouse operation not smooth, but also increases the storage cost. Therefore, it is necessary for manufacturing enterprises to deploy a WMS warehouse management system
eight hundred and forty-eight
one
1 year ago (2024-02-20)

How to select MES system? Main contents of MES production management

Original title: How to select MES system? The main content of MES production management Production and manufacturing activities are the core activities of manufacturing enterprises. As the entity unit of manufacturing activities, planning objectives and the realization of enterprise value, workshop management is the focus of enterprise management. With the rapid development of manufacturing industry, MES is the focus and current hot spot of manufacturing enterprise information automation system
nine hundred and sixty-four
zero
1 year ago (2024-02-20)

The WeChat management system can manage the information content of WeChat more effectively. Let's manage the data of WeChat here

Original title: WeChat management system manages the information content of WeChat more effectively. Many enterprises will assign work to WeChat, because no one wants their personal WeChat to mix work and other related knowledge, but someone will always use the company's loopholes to do something harmful to the company's interests. Don't think such things are rare. I learned about a media financing company in Shenzhen. They have 3
seven hundred and seventy-seven
zero
1 year ago (2024-02-19)

Student electronic file management system, three-dimensional communication space of "home", "school" and "community", and school electronic files

In the era of big data, in the process of recording the growth of students, the school reexamines the management of student files, moves with the times, and promotes the reform of student growth files with new thinking. "Electronic files of primary and secondary school students' growth records" have been gradually introduced into educational management, and show vigorous vitality. Student file management system is an indispensable part of the school, its content for the school
five hundred and seventy-one
zero
1 year ago (2024-02-19)

Ruizhe Information: Select Sitecore? Or... just six steps to choose a content management system, Ruizhe Information Technology Service Co., Ltd

The content management system (CMS) is an important part of the success of website construction. At present, the content management system on the network is relatively complex. It is not easy to find a good content management system that is very suitable for the current digital marketing environment. Before we compare the functionality, operation, scalability, security and other specific performance of the major CMS systems, we need to achieve
three hundred and forty-three
zero

comment

0 people have participated in the review

Scan code to add WeChat

contact us

WeChat: Kuzhuti
Online consultation: