如何使用JQuery Flipbox Slider在Magnific Popup中获取帖子ID

时间:2019-07-02 11:25:34

标签: jquery wordpress magnific-popup

我正在尝试使用Flipbox slider打开Magnific弹出窗口后创建一个滑块,我面临的问题是我在第一页中获得了所有索引元素,并且Flipbox应该将每个索引读为(一侧),但不起作用。

那么,如何从Magnific弹出实例中获取每个帖子的一面?

非常感谢您的帮助。

 jQuery(document).ready( function($) {

    var $button = $(".popup-modal-story");
    var $modal = $(".box");

    $button.on("click", function (event) {
        var index = $(this).index();
        var flipbox;

        $.magnificPopup.open({
            type: 'inline',
            preloader: false,
            midClick: true,
            removalDelay: 400,
            mainClass: 'mfp-fade',
            closeOnContentClick: false,
            closeOnBgClick: true,
            focus: '.image',
            gallery: {
                enabled: true,
                navigateByImgClick: false,
                preload: [0,1]
            },
            items: {
                src: $modal
            },
            callbacks: {
                open: function () {
                    flipbox = this.content.flipbox({
                        vertical: false,
                        width: $('.box').width(),
                        height: $('.box').height(),
                        index: index,
                        animationDuration: 400,
                        animationEasing: 'ease',
                        autoplay: true,
                        autoplayReverse: false,
                        autoplayWaitDuration: 3000,
                        autoplayPauseOnHover: false
                    });
                    console.log(flipbox);
                },
                afterClose: function () {
                    $('.box').flipbox('destroy');
                    console.log(flipbox);
                }
            }
        });

        event.preventDefault();
    });

});

故事模板

<?php
    global $post;

    $args = array(
        'post_type' => 'stories',
        'post_status'    => 'publish'
    );

    $story_posts = new WP_Query( $args );

    if ( $story_posts->have_posts() ) {
        while ( $story_posts->have_posts() ) {
            $story_posts->the_post(); 
?>

<a class="popup-modal-story" href="#test-modal-3-<? the_ID(); ?>"> </a>

<div id="test-modal-3-<? the_ID(); ?>" data-id="<? the_ID(); ?>" class="box white-popup-block modal mfp-hide">

    //the post content

</div>

<?php 

    }

        } else {
            echo '<p>There are no stories yet</p>';
    }
wp_reset_postdata();

?>
function slider_scripts() {
        global $post;

    wp_enqueue_style('flipbox', get_stylesheet_directory_uri().'/css/jquery.flipbox.css', array());
    wp_enqueue_script('flipbox', get_stylesheet_directory_uri().'/custom-js/jquery.flipbox.js', array());
        wp_localize_script('flipbox', 'flipbox_script_vars', array(
                'postID' => $post->ID
            )
        );
}
add_action( 'wp_enqueue_scripts', 'slider_scripts' );

1 个答案:

答案 0 :(得分:0)

您是否尝试未成功获取帖子ID,是问题所在吗?如果是这样,则可以使用标准的post对象。另外,您可能需要稍微调整回显语句以包含文本,而不是仅回显ID,这可能会更易于管理。

<?php
    global $post;

    $args = array(
        'post_type' => 'stories',
        'post_status'    => 'publish'
    );

    $story_posts = new WP_Query( $args );

    if ( $story_posts->have_posts() ) {
        while ( $story_posts->have_posts() ) {
            $story_posts->the_post(); 
?>

<a class="popup-modal-story" href="<?php echo '#test-modal-3-' . $post->ID; ?>"> </a>

<div id="<?php echo 'test-modal-3-' . $post->ID; ?>" data-id="<?php echo $post->ID; ?>" class="box white-popup-block modal mfp-hide">

    //the post content

</div>

<?php 

    }

        } else {
            echo '<p>There are no stories yet</p>';
    }
wp_reset_postdata();

?>