我正在使用elementor插件,并对插件(shortcode)进行编码以获取帖子数据。我想用滑动滑块创建插件,但无法正常工作。它显示数据,但是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,
}
}
});
});
这是结果