好的,我试着在我的自定义主题中实现一个fancybox灯箱。
我将脚本排入函数队列。php:
function ikos_add_lightbox() {
wp_enqueue_script( \'fancybox\', get_template_directory_uri() . \'/inc/lightbox/js/jquery.fancybox.pack.js\', array( \'jquery\' ), false, true );
wp_enqueue_script( \'lightbox\', get_template_directory_uri() . \'/inc/lightbox/js/lightbox.js\', array( \'fancybox\' ), false, true );
wp_enqueue_style( \'lightbox-style\', get_template_directory_uri() . \'/inc/lightbox/css/jquery.fancybox.css\' );
}
add_action( \'wp_enqueue_scripts\', \'ikos_add_lightbox\' );
在我的灯箱js中:
(function($) {
// Initialize the Lightbox for any links with the \'fancybox\' class
$(".fancybox").fancybox();
// Initialize the Lightbox automatically for any links to images with extensions .jpg, .jpeg, .png or .gif
$("a[href$=\'.jpg\'], a[href$=\'.png\'], a[href$=\'.jpeg\'], a[href$=\'.gif\']").fancybox();
// Initialize the Lightbox and add rel="gallery" to all gallery images when the gallery is set up using [gallery link="file"] so that a Lightbox Gallery exists
$(".gallery a[href$=\'.jpg\'], .gallery a[href$=\'.png\'], .gallery a[href$=\'.jpeg\'], .gallery a[href$=\'.gif\']").attr(\'rel\',\'gallery\').fancybox();
// Initalize the Lightbox for any links with the \'video\' class and provide improved video embed support
$(".video").fancybox({
maxWidth : 800,
maxHeight : 600,
fitToView : false,
width : \'70%\',
height : \'70%\',
autoSize : false,
closeClick : false,
openEffect : \'none\',
closeEffect : \'none\'
});
})(jQuery);
所以这应该像js中的注释一样工作。。。我的库代码正在尝试使用lightbox:
<?php
if ( $attachments = get_children( array(
\'post_type\' => \'attachment\',
\'post_mime_type\'=>\'image\', //return all image attachment only
\'numberposts\' => -1, //get all the attachments
\'post_parent\' => $post->ID
)));
foreach ($attachments as $attachment) {
$src = wp_get_attachment_image_src( $attachment->ID, full);
$html = \'<a class="fancybox" href="\'.$src[0].\'">\';
$html .= wp_get_attachment_image( $attachment->ID, \'gallery-thumb\') .\'</a>\';
echo $html;
}
?>
它应该可以与任何人一起使用。jpg。gif或。png但没有,因为我的图片都是。jpg。所以我增加了这个类。fancybox到链接,什么都没有。。。所以我甚至尝试添加gallery rel,但什么都没有发生。。。
控制台日志显示无错误:/
非常感谢您的帮助!