我正试着在我的主题中加载magnific popup,但是它不工作,我不确定为什么。当我单击图像时,它只显示标准的原始图像。
当我用html构建它的时候,它工作得很好,但是在wordpress中它似乎不能工作。
我确实用alert (‘hi’)测试了main.js和jquery.magatio-popup.min.js;它们都发出了警告,所以它们都在工作。
我的入队文件似乎是正确的,尽管我不确定我在页面模板中为图像链接使用了正确的'the_post_thumbnail_url();‘函数?
任何人都能发现问题吗?
仅供参考:我不想使用插件。
我的目录的图像

将文件入队
function add_theme_scripts()
{
wp_enqueue_style('magnific-popup', get_stylesheet_uri() . '/assets/css/magnific-popup/magnific-popup.css', true);
wp_enqueue_style('style', get_stylesheet_uri());
wp_enqueue_script('jquery-3.5.1', get_template_directory_uri() . '/assets/js/jquery-3.5.1.min.js', '3.5.1', true); /*when uploaded to server, wordpress already has jquery*/
wp_enqueue_script('magnific-popup', get_template_directory_uri() . '/assets/js/magnific-popup/jquery.magnific-popup.min.js', '1.1.0', true);
wp_enqueue_script('main', get_template_directory_uri() . '/assets/js/main.js', true);
}
add_action('wp_enqueue_scripts', 'add_theme_scripts');我的页面-project.php
<?php
/**
* Template Name: Project Page
* Template Post Type: post
*/
get_header();
?>
<div class="content">
<h1 class="title main-title"><?php the_title(); ?></h2>
<?php if (have_posts()) : while (have_posts()) : the_post();
the_content();
endwhile;
endif; ?>
<?php
$projects = new WP_Query(array(
'posts_per_page' => 5,
'post_type' => 'projects',
)); ?>
<div class="the-content">
<?php if ($projects->have_posts()) : while ($projects->have_posts()) : $projects->the_post(); ?>
<div class="post-content">
<div class="project-images">
<?php if (has_post_thumbnail()) : ?>
<a class="popup" href="<?php the_post_thumbnail_url(); ?>">
<img src="<?php the_post_thumbnail_url(); ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
</a>
<?php endif; ?>
</div>
<h2 class="title"><?php the_title(); ?></h2>
<?php endwhile;
else : _e('Sorry, no posts seem to have been found');
endif; ?>
</div>
</div>
<div class="step-back">
<a href="<?php echo site_url('/'); ?>">home</a></p>
</div>来自main.js的js文件
// magnific popup
jQuery('.project-images').magnificPopup({
delegate: 'a',
type: 'image',
gallery: {
enabled: true,
navigateByImgClick: true,
preload: [0, 3], // Will preload 0 - before current, and 1 after the current image
tPrev: 'Previous', // title for left button
tNext: 'Next', // title for right button
},
removalDelay: 300,
closeOnContentClick: true,
midClick: true,
mainClass: 'mfp-fade',
callbacks: {
buildControls: function () {
// re-appends controls inside the main container
this.contentContainer.append(this.arrowLeft.add(this.arrowRight));
}
}
});发布于 2021-08-27 21:29:18
我遇到了弹出式窗口无法工作的问题,对我来说,这是一个jQuery冲突。结果发现我需要使用一个不同的jQuery版本。以下是对我起作用的方法。
在添加magnific popup.min.css之后,我必须注销现有的jQuery并将3.5.1版入队。请注意,依赖项是jquery。这是一个documentation参考。
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', get_template_directory_uri() . '/js/jquery3.js');
wp_enqueue_script( 'jquery', get_template_directory_uri() . '/js/jquery3.js');
wp_enqueue_script( 'magnific-popup', get_template_directory_uri() . '/js/jquery.magnific-popup.min.js', array('jquery'), '', true );
wp_enqueue_script( 'magnific-popup-init', get_template_directory_uri() . '/js/jquery.magnific-popup.init.js', array('jquery'), '', true );
wp_enqueue_script( 'custom', get_template_directory_uri() . '/js/custom.js', array('jquery'), '', true );https://stackoverflow.com/questions/64411681
复制相似问题