WordPress教程

Elementor 文章列表模块添加视频弹出和无限加载效果

阿里云

视频弹出和瀑无限加载效果是 Elementor 高级版中包含的页面效果,但是只为这个效果就去购买高级版非常没有性价比。其实 Elementor 免费版也是可以实现这个效果的,而且并不需要太多代码。

视频弹出效果和无限加载效果肯定是通过 JavaScript 实现的,在本文所提到的案例中,我们使用的是 Magnific Popup 和 jQuery Infinite Ajax Scroll 这两个库,首先我们需要引入这两个库(这两个库我们已经在主题中注册过了,所以我们在这里省略了注册的代码)。

也想出现在这里?联系我们
创客主机
  1. add_action('wp_enqueue_scripts', function ()
  2. {
  3.     if (is_page(178)) {
  4.         wp_enqueue_script('wprs-jquery-ias');
  5.         wp_enqueue_style('wprs-magnific-popup');
  6.         wp_enqueue_script('wprs-magnific-popup');
  7.     }
  8. });

因为我们只需要在某个页面中实现这两个效果,所以在添加代码的时候,我们需要做一个判断,只有当前页面是我们所需的页面时,才添加这两个 JS 库。

添加 Popup 和无限加载效果 JS 代码到所需的页面,因为我们已经在上面的代码中引入了所需的 JavaScript 库,我们直接把实现这两个效果的 JS 实例代码添加到对应页面的页脚即可。当然,如果你喜欢,也可以添加到网站的 JS 文件中统一管理。

  1. add_action('wp_footer', function ()
  2. {
  3.     if ( ! is_page(178)) {
  4.         return;
  5.     }
  6.     ?>
  7.     <script>
  8.         jQuery(document).ready(function($) {
  9.             function activatePopup() {
  10.                 $('#feedback-videos .elementor-post__thumbnail__link').magnificPopup({
  11.                     disableOn   : 700,
  12.                     type        : 'iframe',
  13.                     mainClass   : 'mfp-fade',
  14.                     removalDelay: 160,
  15.                     preloader   : false,
  16.                     fixedContentPos: false,
  17.                 });
  18.             }
  19.  
  20.             activatePopup();
  21.  
  22.             var ias = jQuery.ias({
  23.                 container : '.elementor-posts-container',
  24.                 item      : 'article.elementor-post',
  25.                 pagination: 'nav.elementor-pagination',
  26.                 next      : 'nav.elementor-pagination a.next',
  27.             });
  28.  
  29.             ias.extension(new IASSpinnerExtension());
  30.             ias.extension(new IASTriggerExtension({offset: 2}));
  31.             ias.extension(new IASNoneLeftExtension({text: 'You reached the end'}));
  32.             ias.extension(new IASPagingExtension({text: 'View More Videos'}));
  33.             ias.extension(new IASHistoryExtension({prev: '#pagination a.prev'}));
  34.  
  35.             ias.on('rendered', function(items) {
  36.                 activatePopup();
  37.             });
  38.         });
  39.     </script>
  40. <?php });

有了上面的代码,页面列表无限加载的效果已经实现了,但是视频弹出还需要进一步处理才能实现。

修改文章链接为视频链接,Magnific Popup 弹出的视频是直接使用的链接中的视频地址,所以,我们需要修改列表中的文章链接为视频链接地址。我们可以通过 post_type_link 这个 Filter 来修改文章链接地址,下面的代码中,我们把 「feedback」文章类型的链接修改为 「_video_url」这个文章自定义字段中设置的视频链接。

  1. add_filter('post_type_link', function ($permalink, $post, $leavename)
  2. {
  3.     if ($post->post_type === 'feedback') {
  4.         $video_url = get_post_meta($post->ID, '_video_url', true);
  5.  
  6.         if ($video_url) {
  7.             $permalink = $video_url;
  8.         }
  9.     }
  10.  
  11.     return $permalink;
  12. }, 999, 3);

至此,在 Elementor 中实现视频弹出和无限加载的功能已经全部实现,主要使用了 wp_enqueue_scripts Action 来加载 JS 库,使用 wp_footer Action 来添加实例 JS 代码,使用了 post_type_link Filter 来修改文章链接为视频链接,全程没有修改 WordPress 或 Elementor 的核心代码,所以不会影响 WordPress 或 Elementor 升级,兼容性也不错。

Elementor 文章列表模块添加视频弹出和无限加载效果

已有 404 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!