Javascript 在短代码wordpress中打开/关闭自动滚动切换到owl转盘

Javascript 在短代码wordpress中打开/关闭自动滚动切换到owl转盘,javascript,php,wordpress,owl-carousel,wordpress-shortcode,Javascript,Php,Wordpress,Owl Carousel,Wordpress Shortcode,这是我的函数,显示猫头鹰旋转木马 function showpost_func( $atts ) { $atts = shortcode_atts( array( 'category' => '', 'show' => '15', ), $atts, 'showpost' ); ob_start(); if ($atts['category']) { $cc_cat = esc_attr($atts[

这是我的函数,显示猫头鹰旋转木马

function showpost_func( $atts ) {
    $atts = shortcode_atts( array(
        'category' => '',
        'show' => '15',
    ), $atts, 'showpost' );

    ob_start();

    if ($atts['category']) {
        $cc_cat = esc_attr($atts['category']);
        $cc_cat_not = '';
    } else {
        $cc_cat = '';
        $cc_cat_not = '102';
    }

    $the_query = new WP_Query( array( 
        'post_status' => 'publish',
        'post_type' => 'post',
        'category_name' => $cc_cat,
        'orderby' => 'date',
        'order' => 'DESC',
        'posts_per_page' => esc_attr($atts['show']),
        'category__not_in' => $cc_cat_not,
        'ignore_sticky_posts' => 1
    ) );

    if ( $the_query->have_posts() ) {
        echo '<div class="showcustompost"><div class="owl-carousel">';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();

            $featured_video = get_field('video_url');

            if ( has_post_thumbnail() ) {
                $cimg_url = get_the_post_thumbnail_url();
            } else {
                $cimg_url = get_stylesheet_directory_uri().'/images/no-image.jpg';
            }

            if (has_category('video',$post->ID)) {
                echo '<div><a href="'.get_the_permalink().'"></a>'; ?>
                <div style="padding:56.25% 0 0 0;position:relative;"><iframe src="https://player.vimeo.com/video/<?=$featured_video?>?color=a894ae&title=0&byline=0&portrait=0" style="position:absolute;top:0;left:0;width:100%;height:100%;" frameborder="0" allow="autoplay; fullscreen" allowfullscreen></iframe></div><script src="https://player.vimeo.com/api/player.js"></script>
                <?php
                echo '<div class="scp_title">'.get_the_title().'</div></div>';  
            } else {
                echo '<div><a href="'.get_the_permalink().'"></a><div class="scp_bimage" style="background:url('.$cimg_url.') no-repeat;"></div><div class="scp_title">'.get_the_title().'</div></div>';    
            }

        }
        echo '</div></div>';
    } else {
        // no posts found
    }
    wp_reset_postdata();
    return ob_get_clean();
}
add_shortcode( 'showpost', 'showpost_func' );
我的问题是如何为每个短代码设置一个选项以使自动滚动停止

像这样,我显示了两个短代码,第一个短代码不应该滚动,第二个将滚动

[showpost category="category-slug" show="15" autoscroll="stop"] 
[showpost category="category-slug" show="15"]

这就是我所做的,很抱歉我不得不分享我所做的,我添加了另一个快捷码属性

'autoscroll' => 'false'
我还向主包装器添加了一个类,以便脚本将指向其主包装器

<div class="showcustompost '.$cc_cat.'">

下面是工作代码

//[showpost category="category-slug" show="15" autoscroll="true/false"] 
function showpost_func( $atts ) {
    $atts = shortcode_atts( array(
        'category' => '',
        'show' => '15',
        'autoscroll' => 'false'
    ), $atts, 'showpost' );

    ob_start();

    if ($atts['category']) {
        $cc_cat = esc_attr($atts['category']);
        $cc_cat_not = '';
    } else {
        $cc_cat = '';
        $cc_cat_not = '102';
    }

    $the_query = new WP_Query( array( 
        'post_status' => 'publish',
        'post_type' => 'post',
        'category_name' => $cc_cat,
        'orderby' => 'date',
        'order' => 'DESC',
        'posts_per_page' => esc_attr($atts['show']),
        'category__not_in' => $cc_cat_not,
        'ignore_sticky_posts' => 1
    ) );

    if ( $the_query->have_posts() ) {
        echo '<div class="showcustompost '.$cc_cat.'"><div class="owl-carousel">';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();

            $featured_video = get_field('video_url');

            if ( has_post_thumbnail() ) {
                $cimg_url = get_the_post_thumbnail_url();
            } else {
                $cimg_url = get_stylesheet_directory_uri().'/images/no-image.jpg';
            }

            if (has_category('video',$post->ID)) {
                echo '<div><a href="'.get_the_permalink().'"></a>'; ?>
                <div style="padding:56.25% 0 0 0;position:relative;"><iframe src="https://player.vimeo.com/video/<?=$featured_video?>?color=a894ae&title=0&byline=0&portrait=0" style="position:absolute;top:0;left:0;width:100%;height:100%;" frameborder="0" allow="autoplay; fullscreen" allowfullscreen></iframe></div><script src="https://player.vimeo.com/api/player.js"></script>
                <?php
                echo '<div class="scp_title">'.get_the_title().'</div></div>';  
            } else {
                echo '<div><a href="'.get_the_permalink().'"></a><div class="scp_bimage" style="background:url('.$cimg_url.') no-repeat;"></div><div class="scp_title">'.get_the_title().'</div></div>';    
            }

        }
        echo '</div></div>';
    } else {
        // no posts found
    }
?>  

<script>
jQuery(document).ready(function(){
    jQuery('.<?=$cc_cat?> .owl-carousel').owlCarousel({
        autoplay:<?=esc_attr($atts['autoscroll'])?>,
        loop:false,
        rewind:true,
        margin:20,
        nav:true,
        responsive:{
            0:{
                items:3
            },
            600:{
                items:3
            },
            1000:{
                items:4
            }
        }
    })
});
</script>

<?php
    wp_reset_postdata();
    return ob_get_clean();
}
add_shortcode( 'showpost', 'showpost_func' );
/[showpost category=“category slug”show=“15”autoscroll=“true/false”]
函数showpost_func($atts){
$atts=短码_atts(数组)(
'类别'=>'',
'显示'=>'15',
'autoscroll'=>'false'
),$atts,'showpost');
ob_start();
如果($atts['category']){
$cc_cat=esc_attr($atts['category');
$cc_cat_not='';
}否则{
$cc_cat='';
$cc_cat_not='102';
}
$thew_query=新的WP_查询(数组(
“发布状态”=>“发布”,
“post_type”=>“post”,
“类别名称”=>$cc\U类别,
'orderby'=>'date',
“订单”=>“描述”,
“每页帖子”=>esc属性($atts['show']),
“类别不在”=>$cc\U类别不在,
“忽略粘贴的帖子”=>1
) );
if($the\u query->have\u posts()){
回声';
while($the\u query->have\u posts()){
$the_query->the_post();
$featured_video=get_字段('video_url');
如果(具有\u post\u缩略图()){
$cimg_url=获取文章缩略图_url();
}否则{
$cimg_url=get_stylesheet_directory_uri()。/images/no image.jpg';
}
如果(有_类别('video',$post->ID)){
回音“”;?>

将调用旋转木马的脚本放入短代码中
//[showpost category="category-slug" show="15" autoscroll="true/false"] 
function showpost_func( $atts ) {
    $atts = shortcode_atts( array(
        'category' => '',
        'show' => '15',
        'autoscroll' => 'false'
    ), $atts, 'showpost' );

    ob_start();

    if ($atts['category']) {
        $cc_cat = esc_attr($atts['category']);
        $cc_cat_not = '';
    } else {
        $cc_cat = '';
        $cc_cat_not = '102';
    }

    $the_query = new WP_Query( array( 
        'post_status' => 'publish',
        'post_type' => 'post',
        'category_name' => $cc_cat,
        'orderby' => 'date',
        'order' => 'DESC',
        'posts_per_page' => esc_attr($atts['show']),
        'category__not_in' => $cc_cat_not,
        'ignore_sticky_posts' => 1
    ) );

    if ( $the_query->have_posts() ) {
        echo '<div class="showcustompost '.$cc_cat.'"><div class="owl-carousel">';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();

            $featured_video = get_field('video_url');

            if ( has_post_thumbnail() ) {
                $cimg_url = get_the_post_thumbnail_url();
            } else {
                $cimg_url = get_stylesheet_directory_uri().'/images/no-image.jpg';
            }

            if (has_category('video',$post->ID)) {
                echo '<div><a href="'.get_the_permalink().'"></a>'; ?>
                <div style="padding:56.25% 0 0 0;position:relative;"><iframe src="https://player.vimeo.com/video/<?=$featured_video?>?color=a894ae&title=0&byline=0&portrait=0" style="position:absolute;top:0;left:0;width:100%;height:100%;" frameborder="0" allow="autoplay; fullscreen" allowfullscreen></iframe></div><script src="https://player.vimeo.com/api/player.js"></script>
                <?php
                echo '<div class="scp_title">'.get_the_title().'</div></div>';  
            } else {
                echo '<div><a href="'.get_the_permalink().'"></a><div class="scp_bimage" style="background:url('.$cimg_url.') no-repeat;"></div><div class="scp_title">'.get_the_title().'</div></div>';    
            }

        }
        echo '</div></div>';
    } else {
        // no posts found
    }
?>  

<script>
jQuery(document).ready(function(){
    jQuery('.<?=$cc_cat?> .owl-carousel').owlCarousel({
        autoplay:<?=esc_attr($atts['autoscroll'])?>,
        loop:false,
        rewind:true,
        margin:20,
        nav:true,
        responsive:{
            0:{
                items:3
            },
            600:{
                items:3
            },
            1000:{
                items:4
            }
        }
    })
});
</script>

<?php
    wp_reset_postdata();
    return ob_get_clean();
}
add_shortcode( 'showpost', 'showpost_func' );