简体   繁体   中英

Magnific-popup not working in my WordPress theme page template

I am trying to load magnific-popup in my theme, however it's not working and I am not sure why. When I click the image it just shows the standard raw image.

When I built this in just html, it worked fine, however in wordpress it doesn't seem to work.

I did test both the main.js and jquery.magnific-popup.min.js with alert('hi'); and both of them spit out the alert, so both of them are working.

My enqueue files seem correct, although I am not sure I am using the correct 'the_post_thumbnail_url();' functions here in my page template for the image link?

Can anyone spot an issue?

FYI: I don't want to use the plugin.

image of my directory

在此处输入图片说明

enqueue files

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');

my page-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>

js file from main.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));
        }
    }
});

I have had this issue with the popup not working and for me it turned out to be a jQuery conflict. It turns out I needed to use a different jQuery version. Here is what worked for me.

After adding the magnific-popup.min.css I had to deregister the existing jQuery and enqueue version 3.5.1. Note the dependency is jquery. Here is a documentation reference.

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 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM