简体   繁体   English

在JS中使用wordpress简码

[英]Use wordpress shortcode in JS

I am putting up a website using WordPress. 我正在使用WordPress建立一个网站。 In the template I use, a part of the html code is made in JS. 在我使用的模板中,一部分html代码是用JS制作的。 I want to use a slideshow in that specific part of the website. 我想在网站的特定部分使用幻灯片播放。 I have a shortcode for that slideshow. 我有该幻灯片的简码。 I searched in loads of documentation of the theme, but they're saying that it's impossible without major customizations. 我搜索了大量有关该主题的文档,但是他们说如果没有重大定制就不可能实现。 I still think there should be a way around it, but I don't know how. 我仍然认为应该有解决的办法,但是我不知道如何。 So my question is how to use the shortcode in JS. 所以我的问题是如何在JS中使用简码。

The shortcode: 简码:

[metaslider id=274]

The JS for creating the HTML structure: 用于创建HTML结构的JS:

Preview.prototype = {
        create : function() {
            // create Preview structure:
            this.$title = $( '<h3></h3>' );
            this.$description = $( '<p></p>' );
            this.$href = $( '<div><a href="#">Visit website</a></div>' );
            this.$details = $( '<div class="og-details"></div>' ).append( this.$title, this.$description, this.$href);
            this.$loading = $( '<div class="og-loading"></div>' );
            this.$fullimage = $( '<div class="og-fullimg"></div>' ).append( this.$loading );
            this.$closePreview = $( '<span class="og-close"></span>' );
            this.$previewInner = $( '<div class="og-expander-inner"></div>' ).append( this.$closePreview, this.$fullimage, this.$details );
            this.$previewEl = $( '<div class="og-expander"></div>' ).append( this.$previewInner );
            // append preview element to the item
            this.$item.append( this.getEl() );
            // set the transitions for the preview and the item
            if( support ) {
                this.setTransition();
            }
        }

(one of) My futile attempt(s): 我徒劳的尝试之一:

this.$fullimage = $( '<div class="og-fullimg">' . <?php echo do_shortcode("[metaslider id=274]"); ?> . '</div>' ).append( this.$loading );

I hope there's a way to solve my problem... 我希望有办法解决我的问题...

A slideshow is JS script. 幻灯片是JS脚本。 You may init slideshow without using any shortcode. 您可以在不使用任何短代码的情况下启动幻灯片放映。 Read the documentation folowing to your slideshow script. 阅读下面的幻灯片脚本文档。

PS. PS。 Metaslider uses Flexslider script Metaslider使用Flexslider脚本

I understood that you just have to put up an unordered list with images and then you call it in JS with the Flexslider and it makes a slideshow of the list. 我了解到,您只需要建立一个无序的图像列表,然后使用Flexslider在JS中调用它,然后对列表进行幻灯片显示。 So I tried something like this: 所以我尝试了这样的事情:

this.$fullimage = $( '<div class="og-fullimg"><ul>' +
            '<li><img src="wp-content/uploads/2015/03/SurveyApp-500x370.png" alt="testImg">I DO NOT UNDERSTAND THIS ****!!!!</li>' +
            '<li><img src="wp-content/uploads/projects/project' + 
                 this.$item.index() + '-slide2.png"></li>' +
            '</ul></div>' ).append( this.$loading );

But that won't work. 但这行不通。 The images I put in the list just don't show.. In my HTML structure, I get this: 我放入列表中的图像只是不显示。在我的HTML结构中,我得到了:

<ul>
    <li>I DO NOT UNDERSTAND THIS ****!!!!</li>
    <li></li>
</ul>

What's up with that? 那是怎么回事?

I made it work to put the images in a list. 我努力将图像放入列表中。 So in the JS file we were talking about, I did the following: 因此,在我们正在讨论的JS文件中,我做了以下工作:

this.$fullimage = $( '<div class="og-fullimg"><div class="flexslider" id="flexslider"><ul class="slides" id="project' + this.$item.index() + '"></ul></div></div>' ).append( this.$loading );

And in another JS file, I searched for the class of the < ul > and made it generate te list items like this: 在另一个JS文件中,我搜索了<ul>的类,并使其生成如下列表项:

$('.og-grid li a').click(function() {
    setTimeout(function() {
        var expander = document.getElementsByClassName('og-expander');
        if (expander.length > 0) {
            var lists = document.getElementsByClassName('slides');
            var list = lists[0];
            var i;
            for (i = 0; i < 5; i++) {
                var img = document.createElement('img');
                img.src = "http://*my-link*/wp-"
                + "content/uploads/projects/" + list.id + "-slide" 
                + (i+1) + ".png";
                img.alt = "slide " + (i+1) + " van " + list.id;
                var listItem = document.createElement('li');
                listItem.appendChild(img);
                list.appendChild(listItem);
            }
        }
    }, 10);
});

That part works and gives me the list of images I want. 该部分有效,并给了我所需的图像列表。 Now I wanted to add the flexslider. 现在,我想添加flexslider。 So I followed the steps on this site: http://www.woothemes.com/flexslider/ 因此,我按照此网站上的步骤操作: http : //www.woothemes.com/flexslider/

It does something with the lay-out, so the css works, but the JS of flexslider doesn't. 它对布局有作用,所以CSS可以工作,但是flexslider的JS不能。 I get the following error: http://i.imgur.com/Cpkhxw7.png 我收到以下错误: http : //i.imgur.com/Cpkhxw7.png

This is my HTML structure now: 现在是我的HTML结构:

<div class="flexslider" id="flexslider">
    <ul class="slides" id="project2">
        <li><img src="http://*my-link*/wp-content/uploads/projects/project2-slide1.png" alt="slide 1 van project2"></li>
        <li><img src="http://*my-link*/wp-content/uploads/projects/project2-slide2.png" alt="slide 2 van project2"></li>
        <li><img src="http://*my-link*/wp-content/uploads/projects/project2-slide3.png" alt="slide 3 van project2"></li>
        <li><img src="http://*my-link*/wp-content/uploads/projects/project2-slide4.png" alt="slide 4 van project2"></li>
        <li><img src="http://*my-link*/wp-content/uploads/projects/project2-slide5.png" alt="slide 5 van project2"></li>
    </ul>
</div>

It Works! 有用!

I put the JS of the flexslider in the scripts file where I also put the click function I showed you earlier. 我将flexslider的JS放到了脚本文件中,在这里我还放了我前面展示的click函数。 Made some changes in it and it worked! 对其进行了一些更改,就可以了! So now it looks like this: 所以现在看起来像这样:

/* -- PROJECT SLIDESHOW -- */
$('.og-grid li a').click(function() {
    setTimeout(function() {
        var expander = document.getElementsByClassName('og-expander');
        if (expander.length > 0) {
            var lists = document.getElementsByClassName('slides');
            var list = lists[0];
            var i;
            for (i = 0; i < 5; i++) {
                var img = document.createElement('img');
                img.src = "http://*my-link*/wp-content/uploads/"
                + "projects/" + list.id + "-slide" + (i+1) + ".png";
                img.alt = "slide " + (i+1) + " van " + list.id;
                var listItem = document.createElement('li');
                listItem.appendChild(img);
                list.appendChild(listItem);
            }
            $('.flexslider').flexslider({
                animation: "slide",
                slideDirection: "horizontal",
                slideshow: false,
                slideshowSpeed: 7000,
                animationDuration: 600,
                controlNav: true,
                directionNav: true,
                keyboardNav: false,
                mousewheel: false,
                prevText: "Vorige",
                nextText: "Volgende",
                pausePlay: false,
                pauseText: "Pauze",
                playText: "Afspelen",
                randomize: false,
                animationLoop: true,
                pauseOnHover: false
            });
        }
    }, 10);
});

The lay-out is pretty ugly, but I can fix that with some custom CSS. 布局很难看,但是我可以使用一些自定义CSS来解决。

I'm so happy!! 我很开心!! Thanks to 'tutankhamun' for giving me the idea of working with flexslider! 感谢“ tutankhamun”给了我使用flexslider的想法! Take that support guys who told me it was impossible without a freelancer :) 拿那些告诉我的支持者来说,没有自由职业者是不可能的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM