Как исправить плагин шорткода в WordPress (elementor)? - PullRequest
1 голос
/ 13 июня 2019

Я работаю с плагином elementor и кодирую плагин (шорткод) для получения данных постов.Я хочу создать плагин с ползунком Swiper, но он не работает правильно.Он показывает данные, но the_title () и другие функции не загружают данные в слайдер только перед слайдером.Внутри консоли все в порядке.

Это класс

class MRSolutionsSliderWoocommerce
{
    function register() 
    {
        add_action('wp_enqueue_scripts', array($this, 'enqueue'));
        add_action( 'init', array($this, 'create_slider_posttype'));
        add_action( 'init', array($this, 'create_slider_location_tax'));
        add_action('wp_insert_post', array($this, 'set_default_slidermeta'));
        add_shortcode('simpleslider', array($this, 'simple_slider_shortcode'));
    }

    function activate()
    {
        flush_rewrite_rules();
    }

    function deactivate()
    {
        flush_rewrite_rules();
    }

    function enqueue() 
    {
        wp_enqueue_style('swiperCss', plugins_url('/assets/css/swiper.min.css', __FILE__));
        wp_enqueue_script('swiperJs', plugins_url('/assets/js/swiper.min.js', __FILE__));
        wp_enqueue_style('mrSliderWoocommerceStyle', plugins_url('/assets/css/style.css', __FILE__));
        wp_enqueue_script('mrSliderWoocommerceScript', plugins_url('/assets/js/scripts.js', __FILE__));
    }

    function simple_slider_shortcode($atts = null) {

        $no_products = 'Brak produktów na wyprzedaży';

        $ss_atts = shortcode_atts(
            array(
                'slider_type' => '',
                'limit' => 6,
                'on_sale' => 0
            ), $atts, 'simpleslider'
        );

        $slides = array();

        $args = array(
            'post_type' => 'product',
            'meta_key' => $ss_atts['total_sales'],
            'orderby' => 'meta_value_num',
            'posts_per_page' => $ss_atts['limit'],
            'meta_query'     => array(
                'relation' => 'OR',
                array( // Simple products type
                    'key'           => '_sale_price',
                    'value'         => 0,
                    'compare'       => '>',
                    'type'          => 'numeric'
                ),
                array( // Variable products type
                    'key'           => '_min_variation_sale_price',
                    'value'         => 0,
                    'compare'       => '>',
                    'type'          => 'numeric'
                )
            )
        );

        if($ss_atts['on_sale'] == 1) {
            $args = array(
                'post_type' => 'product',
                'posts_per_page' => $ss_atts['limit'],
                'meta_query'     => array(
                    'relation' => 'OR',
                    array( // Simple products type
                        'key'           => '_sale_price',
                        'value'         => 0,
                        'compare'       => '>',
                        'type'          => 'numeric'
                    ),
                    array( // Variable products type
                        'key'           => '_min_variation_sale_price',
                        'value'         => 0,
                        'compare'       => '>',
                        'type'          => 'numeric'
                    )
                )
            );
        } else {
            $args = array(
                'post_type' => 'product',
                'meta_key' => $ss_atts['total_sales'],
                'orderby' => 'meta_value_num',
                'posts_per_page' => $ss_atts['limit']
            );
        }

        $loop = new WP_Query( $args );

        $return_prod = '';
        if($loop->have_posts()) {
            while ( $loop->have_posts() ) : $loop->the_post();
                global $product;
                $slides[] = '
                <article class="swiper-slide">
                    <div class="slide-content">
                        <div class="slide-img">' . woocommerce_get_product_thumbnail() . '</div>
                        <a href="' . get_permalink() . '"><h3 class="slide-title">' . get_the_title() . '</h3></a>
                        <p class="slide-text">' . wp_trim_words(get_the_content(), 15, '...' ) . '</p>
                        <a class="slide-btn" href="' . get_permalink() . '">Wybierz produkt</a>
                    </div>
                </article>';
            endwhile;
            wp_reset_query();

            return '
            <div class="swiper-container">
                <section class="swiper-wrapper">
                    '.implode('', $slides).'
                </section>
            </div>';
        } else {
            return '<h3 class="no-products-slider">' . $no_products . '</h3>';
        }
    }

    function maxContentLength($content)
    {
        // Take the existing content and return a subset of it
        return substr($content, 0, 100);
    }
}

if(class_exists('MRSolutionsSliderWoocommerce')) 
{
    $mrSliderWoocommerce = new MRSolutionsSliderWoocommerce();

    // Call register method
    $mrSliderWoocommerce->register();
}


// activation
register_activation_hook(__FILE__, array($mrSliderWoocommerce, 'activate'));

// deactivation
register_deactivation_hook(__FILE__, array($mrSliderWoocommerce, 'deactivate'));

, а это js

jQuery(document).ready(function () {
    var swiper = new Swiper('.swiper-container', {
        autoplay: {
            delay: 2500,
            disableOnInteraction: false,
        },
        slidesPerView: 3,
        spaceBetween: 20,
        // init: false,
        pagination: {
          el: '.swiper-pagination',
          clickable: true,
        },
        breakpoints: {
          1024: {
            slidesPerView: 2,
            spaceBetween: 10,
          },
          560: {
            slidesPerView: 1,
            spaceBetween: 10,
          }
        }
      });
});

Это результат

enterописание изображения здесь

...