Home Web Front-end JS Tutorial Use jQuery to implement @ ID floating display in WordPress

Use jQuery to implement @ ID floating display in WordPress

Jul 02, 2018 pm 03:31 PM
jquery wordpress Comment

This article mainly introduces the use of JavaScript to implement the function of ID floating display comments in WordPress, which is to display the main content of the comment in a building-in-a-building style comment. Friends who need it can refer to it

For example : A left a message, and B replied to A with @, so B’s reply may be like this:

@A
How much money do you have?

That is, when the mouse is hovering When you stop on @A, A's comment content will be displayed in a floating area.

20151211152525545.png (480×200)

Implementation steps
Here we will take the iNove theme as an example to explain.
1. Save the following code as commenttips.js:

jQuery(document).ready(
 function(){
 var id=/^#comment-/;
 var at=/^@/;
 jQuery('#thecomments li p a').each(
  function() {
  if(jQuery(this).attr('href').match(id)&& jQuery(this).text().match(at)) {
   jQuery(this).addClass('atreply');
  }
  }
 );
 jQuery('.atreply').hover(
  function() {
  jQuery(jQuery(this).attr('href')).clone().hide().insertAfter(jQuery(this).parents('li')).attr('id','').addClass('tip').fadeIn(200);
  }, 
  function() {
  jQuery('.tip').fadeOut(400, function(){jQuery(this).remove();});
  }
 );
 jQuery('.atreply').mousemove(
  function(e) {
  jQuery('.tip').css({left:(e.clientX+18),top:(e.pageY+18)})
  }
 );
 }
)
Copy after login

2. Place the commenttips.js file into the inove/js directory.

3. Append the style code to style.css As follows:

#thecomments .tip {
 background:#FFF;
 border:1px solid #CCC;
 width:605px;
 padding:10px !important;
 padding:10px 10px 0;
 margin-top:0;
 position:absolute;
 z-index:3;
}
#thecomments .tip .act {
 display:none;
}
*+html #thecomments .tip {
 padding:10px 10px 0 !important;
}
Copy after login

4. Add code to call JavaScript in the theme. Open templates/end.php, and add the following code in the line before :
(If you have other plug-ins or have already The jQuery library has been added, so the first line of code does not need to be added.)

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
<script type="text/javascript" src="<?php bloginfo(&#39;template_url&#39;); ?>/js/commenttips.js"></script>
Copy after login

5. Okay, refresh the page with the @ reply, wait until the page is loaded, hover the mouse over the @ reply, You will see the effect.

Why can’t it be displayed across pages?
Because the working principle is that when the mouse moves to @{username}, it will be found on this page The corresponding comment is inserted into the comment list and displayed in absolute position. If the comment is not on this page, the object cannot be found, and of course there will be no subsequent processing.

How to cross pages Get comment information?
If the corresponding comment cannot be found on this page, you can use AJAX to return the comment information queried in the background to the page through the comment ID. When the mouse moves over the @ comment, hover to the user The 'Loading...' prompt box is displayed. If the operation is successful, the found comment will be inserted at the end of the comment list, and the content of the comment will be replaced in the 'Loading...' box.
In other words, the comment that has been loaded The comments will always remain on this page, and there is no need to reload when the mouse moves over the @ comment again.
Let’s take a look at how to handle cross-page comments:

On the current page How to find the corresponding comment through @{username}?

1. Each comment will have an ID, the structure is like: comment-{commentId}, this is to facilitate finding the comment through the anchor point, and at the same time It has also become a necessary condition to complete the @ comment prompt.
2. Each @{username} is actually the anchor point pointing to the comment, and naturally the comment ID can be obtained.

So it is actually very simple, if the comment ID is _commentId, then the corresponding comment can be found in JS through the following code.

document.getElementById(_commentId);
Copy after login

If the target comment can be found, a hidden temporary comment is created, and the target comment is used as its content, in the @{username} attachment Display it; if the target comment is not found, use the ID to find the corresponding comment in the background and perform cross-page processing.

How to load comments across pages?

The essence of cross-page is to dynamically load comments and append the obtained comments to the end of the comment list so that the comments can be found on this page. The difference is that these comments will not be displayed through CSS processing.

You can refer to Figure below. If the comment is not on this page, it will take the red path. After the comment is added to the current page, there will be an action to replace the Loading information in the prompt box with the comment content. When the user hovers the mouse here @{ username}, the comment is already on the current page, so there is no need to load it again. Instead, take the green path and directly bring up the comment prompt box.

20151211152628341.png (509×666)

##Note: The blue part in the picture is background processing, and the yellow part is the focus of the entire loading process.

How to get comments and format them in the background?

Here you can write your own method to process comments The information is formatted, or the formatted HTML can be obtained through the callback method of the comment (WordPress 2.7 or above can define the callback method of the comment).

$comment = get_comment($_GET[&#39;id&#39;]);
custom_comments($comment, null,null);
Copy after login

Note: custom_comments is the method name of my callback function .

JavaScript code

JS code based on jQuery. If you do not use or use other JS frames, please modify it yourself according to the processing ideas. It is recommended to place the code below the comment list.

var id=/^#comment-/;
var at=/^@/;
jQuery(&#39;#thecomments li p a&#39;).each(function() {
 if(jQuery(this).attr(&#39;href&#39;).match(id)&& jQuery(this).text().match(at)) {
 jQuery(this).addClass(&#39;atreply&#39;);
 }
});
jQuery(&#39;.atreply&#39;).hover(function() {
 var target = this;
 var _commentId = jQuery(this).attr(&#39;href&#39;);
 
 if(jQuery(_commentId).is(&#39;.comment&#39;)) {
 jQuery(&#39;<li class="comment tip"></li>&#39;).hide().html(jQuery(_commentId).html()).appendTo(jQuery(&#39;#thecomments&#39;));
 jQuery(&#39;#thecomments .tip&#39;).css({
  left:jQuery().cumulativeOffset(this)[0] + jQuery(this).width() + 10,
  top:jQuery().cumulativeOffset(this)[1] - 22
 }).fadeIn();
 } else {
 var id = _commentId.slice(9);
 jQuery.ajax({
  type:     &#39;GET&#39;
  ,url:     &#39;?action=load_comment&id=&#39; + id
  ,cache:    false
  ,dataType:  &#39;html&#39;
  ,contentType: &#39;application/json; charset=utf-8&#39;
 
  ,beforeSend: function(){
  jQuery(&#39;<li class="comment tip"></li>&#39;).hide().html(&#39;<p class="ajax-loader msg">Loading...</p>&#39;).appendTo(jQuery(&#39;#thecomments&#39;));
  jQuery(&#39;#thecomments .tip&#39;).css({
   left:jQuery().cumulativeOffset(target)[0] + jQuery(target).width() + 10,
   top:jQuery().cumulativeOffset(target)[1] - 22
  }).fadeIn();
  }
 
  ,success: function(data){
  var addedComment = jQuery(data + &#39;</li>&#39;);
  addedComment.hide().appendTo(jQuery(&#39;#thecomments&#39;));
  jQuery(&#39;#thecomments .tip&#39;).html(addedComment.html());
  }
 
  ,error: function(){
  jQuery(&#39;#thecomments .tip&#39;).html(&#39;<p class="msg">Oops, failed to load data.</p>&#39;);
  }
 });
 }
}, function() {
 jQuery(&#39;#thecomments .tip&#39;).fadeOut(400, function(){
 jQuery(this).remove();
 });
});
Copy after login

PHP Code

This code comes from the PhilNa2 theme. It is recommended to append the code to function.php.

function load_comment(){
 if($_GET[&#39;action&#39;] ==&#39;load_comment&#39; && $_GET[&#39;id&#39;] != &#39;&#39;){
 $comment = get_comment($_GET[&#39;id&#39;]);
 if(!$comment) {
  fail(printf(&#39;Whoops! Can\&#39;t find the comment with id %1$s&#39;, $_GET[&#39;id&#39;]));
 }
 
 custom_comments($comment, null,null);
 die();
 }
}
add_action(&#39;init&#39;, &#39;load_comment&#39;);
Copy after login
The above is the entire content of this article. I hope it will be helpful to everyone's learning. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Ajax bootstrap beautifies the web page and implements the code of page loading, deletion and viewing details

jquery in$ Introduction to the implementation of .fn and image scrolling effects

The above is the detailed content of Use jQuery to implement @ ID floating display in WordPress. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to adjust the wordpress article list How to adjust the wordpress article list Apr 20, 2025 am 10:48 AM

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

What are the plugins for wordpress blocking ip What are the plugins for wordpress blocking ip Apr 20, 2025 am 08:27 AM

WordPress IP blocking plugin selection is crucial. The following types can be considered: based on .htaccess: efficient, but complex operation; database operation: flexible, but low efficiency; firewall: high security performance, but complex configuration; self-written: highest control, but requires more technical level.

How to write a header of a wordpress How to write a header of a wordpress Apr 20, 2025 pm 12:09 PM

The steps to create a custom header in WordPress are as follows: Edit the theme file "header.php". Add your website name and description. Create a navigation menu. Add a search bar. Save changes and view your custom header.

How to cancel the editing date of wordpress How to cancel the editing date of wordpress Apr 20, 2025 am 10:54 AM

WordPress editing dates can be canceled in three ways: 1. Install the Enable Post Date Disable plug-in; 2. Add code in the functions.php file; 3. Manually edit the post_modified column in the wp_posts table.

How to change the head image of the wordpress theme How to change the head image of the wordpress theme Apr 20, 2025 am 10:00 AM

A step-by-step guide to replacing a header image of WordPress: Log in to the WordPress dashboard and navigate to Appearance &gt;Theme. Select the topic you want to edit and click Customize. Open the Theme Options panel and look for the Site Header or Header Image options. Click the Select Image button and upload a new head image. Crop the image and click Save and Crop. Click the Save and Publish button to update the changes.

WordPress website account login WordPress website account login Apr 20, 2025 am 09:06 AM

To log in to a WordPress website account: Visit the login page: Enter the website URL plus "/wp-login.php". Enter your username and password. Click "Login". Verification Two-step Verification (optional). After successfully logging in, you will see the website dashboard.

What to do if there is an error in wordpress What to do if there is an error in wordpress Apr 20, 2025 am 11:57 AM

WordPress Error Resolution Guide: 500 Internal Server Error: Disable the plug-in or check the server error log. 404 Page not found: Check permalink and make sure the page link is correct. White Screen of Death: Increase the server PHP memory limit. Database connection error: Check the database server status and WordPress configuration. Other tips: enable debug mode, check error logs, and seek support. Prevent errors: regularly update WordPress, install only necessary plugins, regularly back up your website, and optimize website performance.

How to display wordpress comments How to display wordpress comments Apr 20, 2025 pm 12:06 PM

Enable comments in WordPress website: 1. Log in to the admin panel, go to "Settings" - "Discussions", and check "Allow comments"; 2. Select a location to display comments; 3. Customize comments; 4. Manage comments, approve, reject or delete; 5. Use &lt;?php comments_template(); ?&gt; tags to display comments; 6. Enable nested comments; 7. Adjust comment shape; 8. Use plugins and verification codes to prevent spam comments; 9. Encourage users to use Gravatar avatar; 10. Create comments to refer to

See all articles