WP模板研发中,怎么样给wordpress网站的文案,添加点赞功能?
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>在网上浏览某些网站的<span style="color: black;">文案</span>时,在<span style="color: black;">文案</span>的结尾处,都会有一个点赞的按钮,<span style="color: black;">倘若</span>觉得<span style="color: black;">文案</span>内容非常不错,就<span style="color: black;">能够</span>点击这个“点赞”按钮,给这篇<span style="color: black;">文案</span>进行点赞一下。在wordpress网站的模板主题<span style="color: black;">研发</span>中,<span style="color: black;">咱们</span>可不<span style="color: black;">能够</span><span style="color: black;">亦</span>为<span style="color: black;">咱们</span>wordpress网站的<span style="color: black;">文案</span>添加上这个“点赞”功能呢?答案是肯定的。<span style="color: black;">那样</span>,<strong style="color: blue;"><span style="color: black;">怎么样</span>给wordpress网站的<span style="color: black;">文案</span>添加“点赞”功能呢?</strong>看我是<span style="color: black;">怎么样</span>实现的吧。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p26-sign.toutiaoimg.com/pgc-image/33626ec570ca4554809df74eae933174~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728358596&x-signature=12TeSiUusS6pLqv5WnD4nY6oP1k%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">第1</span>步:在wordpress模板的头部文件header.php中引入jquery。</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">为了在点击“点赞”按钮时,能够向后台传递数据,<span style="color: black;">咱们</span><span style="color: black;">这儿</span>要用到jquery的AJAX处理函数,<span style="color: black;">因此</span>,<span style="color: black;">咱们</span>要先在wordpress模板的头部文件中引入这个jquery文件,我<span style="color: black;">这儿</span>是jquery1.7.2版本,其它版本<span style="color: black;">亦</span><span style="color: black;">能够</span>。代码如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><script src="<?php bloginfo("template_url"); ?>/js/jquery-1.7.2.min.js"></script></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">第二步:在wordpress<span style="color: black;">文案</span>内容下方添加“点赞”按钮。代码如下:</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><div class="item single_praise">点赞:<span class="dashicons-before dashicons-heart"></span><span class="praise_num"><?php$praise_num = get_post_meta($post->ID,post_praise); //获取点赞数echo $praise_num ? $praise_num : 0;?></span></div></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">第三步:添加点击“点赞”的事件处理代码。</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>要给这个“点赞”按钮添加一个事件,只要一点击这个“点赞”按钮,就弹出相应的事件处理,把当前<span style="color: black;">文案</span>的ID号<span style="color: black;">经过</span>AJAX传递到single_praise.php文件中。代码如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><script type="text/javascript">$(function(){var pid = <?php echo $post->ID; ?>; //<span style="color: black;">文案</span>IDvar user = <?php echo wp_get_current_user()->user_login; ?>; //当前登录的用户名</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$(".single_praise").click(function(){</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$.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;$(".praise_num").html(num); //把点赞重新写入</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}})})})</script></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/c49307345e104dde9d3f46569c95f304~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728358596&x-signature=YQSD5ztlnzseU%2FTfOdxZGc4c8NE%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">第四步:向wordpress数据库中的对应<span style="color: black;">文案</span>添加“点赞”数据。</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在wordpress模板目录下的include目录的下面,创建一个single_praise.php文件,用来接收AJAX传递过来的数据,<span style="color: black;">而后</span>,向wordpress数据库里添加或修改数据(<span style="color: black;">文案</span>的点赞数据)。代码如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><?phpdefine(BASE_PATH,str_replace( \\ , / , realpath(dirname(__FILE__)./../../../../)));//获取根目录require(BASE_PATH./wp-load.php );$postid = esc_sql($_POST);$user = esc_sql($_POST);$u_ip = $_SERVER["REMOTE_ADDR"];if($postid==0 ){ //<span style="color: black;">倘若</span><span style="color: black;">文案</span>ID=0exit("<span style="color: black;">违法</span>操作");}//向数据库中添加点赞数据$praise_num = get_post_meta($postid,post_praise);$user_views = !empty($praise_num) ? $praise_num : 0;update_post_meta($postid,post_praise,$user_views+1);print_r($praise_num); exit;</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/9b4c740b2efa4dfcbb1e6ddb47272cfe~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728358596&x-signature=VEd4R69pAPsgaoDGlF9yym4QUGA%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">通过上面的五步,<span style="color: black;">咱们</span>就给wordpress网站的<span style="color: black;">文案</span>添加了点赞功能,<span style="color: black;">咱们</span>每点击一次,就会给<span style="color: black;">文案</span>的点赞数据+1。<span style="color: black;">这般</span>,<span style="color: black;">咱们</span>就基本上完<span style="color: black;">成为了</span>操作。<span style="color: black;">然则</span>,有一个美中不足的<span style="color: black;">地区</span>,同一个用户,<span style="color: black;">倘若</span>连续点击这个“点赞”按钮,就会<span style="color: black;">一直</span>地<span style="color: black;">增多</span>点赞数,<span style="color: black;">这般</span>不太友好<strong style="color: blue;">。<span style="color: black;">通常</span><span style="color: black;">状况</span>下,<span style="color: black;">咱们</span>只让一个用户一天之内,只能点赞一次,这个功能,<span style="color: black;">咱们</span>在下一章中再做相应的介绍,敬请期待,<span style="color: black;">咱们</span>下一章再见</strong>。</p>
你字句如珍珠,我珍藏这份情。 外链论坛的成功举办,是与各位领导、同仁们的关怀和支持分不开的。在此,我谨代表公司向关心和支持论坛的各界人士表示最衷心的感谢!
页:
[1]