Real implementation of WordPress lazyload picture Delay Loading instance

Source: Internet
Author: User
Tags regular expression

Lazyload Picture Delay Loading error
Code Installation
Jquery.lazyload.js is a jquery plug-in, so you must first load jquery, and now the skin will use jquery. No students can insert the following code between:

The code is as follows Copy Code

<script type= "Text/javascript" src= "Http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" ></ Script>

Then insert this code:

The code is as follows Copy Code

<script type= "Text/javascript" src= "<?php bloginfo" (' template_directory ');? >/js/jquery.lazyload.js "></script>
<script type= "Text/javascript" >
Delay Image
JQuery (document). Ready (
Function ($) {
$ ("img"). Lazyload ({
Placeholder: "<?php bloginfo" (' Template_url ');? >/images/loading.gif ",
Effect: "FadeIn"
});
});
</script>

Save the changes.

Where Jquery.lazyload.js and loading.gif are placed in the use of the skin directory, the path is generally/wp-content/themes/and so on the skin/The above code is completely wrong, this picture really has been loaded, Just didn't show up. Below look at a real implementation of the picture delay loading code.


Now, the great gods have solved the problem. The original JQuery Lazyload plugin said to write the image address to the data-original attribute, loading the image address to the URL property to implement the real picture later load (lazyload). This means that you want to modify the HTML structure of the picture.

Before modification:

The code is as follows Copy Code



After modification:

GIF ">


In the above code, img/example.jpg is the actual picture address to display.

The use of JQuery lazyload

The full jquery lazyload picture Delay loading method is set as follows:


First step: Load jquery related files.

You want to load jquery and this plugin. You can use the following code to load these files:

The code is as follows Copy Code


<script src= "Jquery.js" type= "Text/javascript" ></script>
<script src= "Jquery.lazyload.js" type= "Text/javascript" ></script>


Step two: Define the picture structure.

Follow the official advice to define your IMG structure:

The code is as follows Copy Code



Step three: Trigger this plugin to take effect.
If you activate the following, you can use it in the target.

The code is as follows Copy Code
$ ("Img.lazy"). Lazyload ();

Advantages of using jquery lazyload

The advantages have been said too much, here is not to repeat.

Disadvantages of using jquery lazyload

Shortcomings also have, here also do not repeat.

One of the biggest drawbacks here is that this method is not convenient to operate. Because every time you insert a picture by this HTML structure, the workload is a lot more steep, and it can cause the picture not to appear at all. Therefore, young for WordPress wrote a string of the following code, with regular expression to reorganize the IMG tags, put in the theme of the functions.php file directly to use. This still allows you to insert the picture in the way you used to.

Code:

The code is as follows Copy Code


Add_filter (' the_content ', ' lazyload ');
function Lazyload ($content) {
if (!is_feed () | |! Is_robots) {
$content =preg_replace ('//i '," n<noscript>$0</noscript>", $content);
}
return $content;
}


Loading1.gif suggests using absolute paths in the above code, such as Http://www.mzihen.com/images/loading.gif

But this code loading.gif address can not be universal, everyone needs to be modified to their own address, for this I specifically consulted young, revised the code again. Just place the loading.gif in the theme folder.

The code is as follows Copy Code

Add_filter (' the_content ', ' lazyload ');
function Lazyload ($content) {
$loadimg _url=get_bloginfo (' template_directory '). ' /loading.gif ';
if (!is_feed () | |! Is_robots) {
$content =preg_replace ('//i ', "n<noscript>$0</noscript>", $content);
}
return $content;
}


Thanks again for Binaryoung patient help.

Also thanks to @sam touhenda.com classmate's test.

At this point, the picture in WordPress real lazyload delay loading.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.