вкл / выкл автопрокрутки переключить на сову карусели внутри шорткода WordPress - PullRequest
0 голосов
/ 06 марта 2020

У меня здесь есть Моя функция, которая показывает карусель совы

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

1 Ответ

0 голосов
/ 06 марта 2020

Это то, что я сделал, извините, мне пришлось поделиться тем, что я сделал, я добавил еще один атрибут шорткода

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