简体   繁体   English

WordPress 添加<p>标签到我的 javascript

[英]Wordpress add <p> tags to my javascript

I use philantrophy theme, in which I added sliders via shortcodes in a page then it automatically inserted p tags inside my javascript.我使用 philantrophy 主题,其中我通过页面中的短代码添加了滑块,然后它会自动在我的 javascript 中插入 p 标签。

below code shows p tag added in my javascript code:下面的代码显示了在我的 javascript 代码中添加的 p 标签:

<script>
jQuery(function($) {
    jQuery('.main-slider, .page-header').prepend('<img src="http://localhost/projects/new_site/wp-content/uploads/2015/07/sketch.png" alt="" id="testimage" class="hidden">');
});</p>
<p>    jQuery(document).ready(function($) {</p>
<p>        jQuery('.main-carousel').prepend('<img src="http://localhost/projects/new_site/wp-content/uploads/2015/07/sketch.png" alt="" class="testimage hidden">');</p>
<p>        jQuery('.testimage').load(function(){
        jQuery(".slider-full .spinner, .slider-full .testimage").remove();
        jQuery(".main-carousel").removeClass('invisible').addClass('animated fadeIn');
    });</p>
<p>        var slider = jQuery('#myCarousel'),
                        animateClass;</p>
<p>        //Brogressbar Slider
        var percent = 0, bar = jQuery('.brogressbar'), interval = 2;</p>
<p>        function progressBarCarousel() {
                bar.css({width:percent+'%'});
                bar.css('transition', '0.2s');
                percent = percent +1;</p>
<p>        }
        var barInterval = setInterval(progressBarCarousel, interval/105);
        slider.carousel({
                interval: interval,
                pause: false
        }).on('slide.bs.carousel', function () {
                            percent=0;
                            bar.css('transition', '0s')
                    });</p>
<p>        slider.find('[data-animate-in]').addClass('animated');</p>
<p>        function animateSlide() {
                slider.find('.item').removeClass('current');</p>
<p>                slider.find('.active').addClass('current').find('[data-animate-in]').each(function () {
                        var $this = jQuery(this);
                        animateClass = $this.data('animate-in');
                        $this.addClass(animateClass);
                });</p>
<p>                slider.find('.active').find('[data-animate-out]').each(function () {
                        var $this = jQuery(this);
                        animateClass = $this.data('animate-out');
                        $this.removeClass(animateClass);
                });
        }
        function animateSlideEnd() {
            slider.find('.active [data-animate-in], .carousel-indicators, .carousel-control').each(function () {
                    var $this = jQuery(this);
                    animateClass = $this.data('animate-in');
                    $this.removeClass(animateClass);
            });
            slider.find('.active [data-animate-in], .carousel-indicators, .carousel-control').each(function () {
                    var $this = jQuery(this);
                    animateClass = $this.data('animate-out');
                    $this.addClass(animateClass);
            });
        }</p>
<p>        slider.find('.invisible').removeClass('invisible');
        animateSlide();</p>
<p>        slider.on('slid.bs.carousel', function () {
                animateSlide();
        });
        slider.on('slide.bs.carousel', function () {
                animateSlideEnd();
        });</p>
<p>        if (Modernizr.touch) {
            slider.find('.carousel-inner').swipe( {
                    swipeLeft: function() {
                        jQuery(this).parent().carousel('prev');
                    },
                    swipeRight: function() {
                        jQuery(this).parent().carousel('next');
                    },
                    threshold: 30
            });
        }
    });</p>
<p></script>

I also tried我也试过

remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );

inside theme's function.php Please help me out to get rid of this.里面主题的function.php 请帮我摆脱这个。 thanks in advance.提前致谢。

Here are some options,这里有一些选项,

  1. Take all the whitespace out of the script so that WordPress does not add <p> tags.去掉脚本中的所有空白,这样 WordPress 就不会添加<p>标签。
  2. Check out this link http://codex.wordpress.org/Using_Javascript查看此链接http://codex.wordpress.org/Using_Javascript

The safe and recommended method of adding JavaScript to a WordPress generated page, and WordPress Theme or Plugin, is by using wp_enqueue_script().将 JavaScript 添加到 WordPress 生成的页面和 WordPress 主题或插件的安全且推荐的方法是使用 wp_enqueue_script()。 This function includes the script if it hasn't already been included, and safely handles dependencies.此函数包含脚本(如果尚未包含),并安全地处理依赖项。

As stated here https://codex.wordpress.org/Function_Reference/wp_enqueue_script如此处所述https://codex.wordpress.org/Function_Reference/wp_enqueue_script

This is the recommended method of linking JavaScript to a WordPress generated page.这是将 JavaScript 链接到 WordPress 生成页面的推荐方法。

Copy and save your script in a new file and copy it's directory.将您的脚本复制并保存在一个新文件中,然后复制它的目录。

Example Usage示例用法

wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );

Link a Theme Script Which Depends on jQuery链接依赖于 jQuery 的主题脚本

wp_enqueue_script('custom-script', get_template_directory_uri() . '/js/custom_script.js', array( 'jquery' ));

There's a trick you could bypass the plugin installation if you do not have that ability.如果您没有这种能力,有一个技巧可以绕过插件安装。

  1. Minify your JavaScript Code into one line.将您的 JavaScript 代码压缩为一行。 You can use any JS minifier available online.您可以使用任何在线可用的 JS 压缩器。
  2. Paste that on your page while like {minified code} to make sure everything is on one line.将其粘贴到您的页面上,同时像 {minified code} 以确保所有内容都在一行上。

Now even if the paragraph tag is added, it does not matter as the script would still execute.现在,即使添加了段落标记,也没有关系,因为脚本仍会执行。

I see its an old question, but I ran into it quite recently.我看到它是一个老问题,但我最近遇到了它。 I got it working without any plugins using the following approach:我使用以下方法在没有任何插件的情况下工作:

<script type="text/javascript">
//<![CDATA[

your code

//]]>
</script>

Live example (2nd code listing): JavaScript in Beitrag einbetten .实时示例(第二个代码清单): Beitrag einbetten 中的 JavaScript Not sure if it is working for all wordpress versions - worth a try I guess.不确定它是否适用于所有 wordpress 版本 - 我猜值得一试。

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

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