打开/关闭自动滚动切换到短代码wordpress内的猫头鹰转盘



我有一个显示猫头鹰转盘的函数

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"]

这是我的猫头鹰旋转木马js代码

jQuery('.owl-carousel').owlCarousel({
    autoplay:true,
    loop:false,
    rewind:true,
    margin:20,
    nav:true,
    responsive:{
        0:{
            items:3
        },
        600:{
            items:3
        },
        1000:{
            items:4
        }
    }
})

我的问题是如何为每个短代码提供选项以使自动滚动停止

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

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

最新更新