1

我的footer.php 中有这个脚本,我喜欢使用wp_enqueue_scripts 将它添加到footer.php。

<script type="text/javascript">
jQuery(window).load(function(){

<?php
if ( is_home() ) {

    if ( option::get('featured_posts_show') == 'on' ) {

        ?>if ( jQuery('#slider li').length > 0 ) {
            jQuery('#slider').flexslider({
                controlNav: false,
                directionNav: false,
                animationLoop: true,
                animation: '<?php echo option::get('slideshow_effect') == 'Slide' ? 'slide' : 'fade'; ?>',
                useCSS: true,
                smoothHeight: false,
                touch: false,
                slideshow: <?php echo option::get('slideshow_auto') == 'on' ? 'true' : 'false'; ?>,
                <?php if ( option::get('slideshow_auto') == 'on' ) echo 'slideshowSpeed: ' . option::get('slideshow_speed') . ','; ?>
                pauseOnAction: true,
                animationSpeed: 600,
                start: function(slider){
                    jQuery('#slider_nav .item').click(function(){
                        var id = getPostIdClass(this);
                        if ( id <= 0 ) return;

                        var index = slider.slides.index( slider.slides.filter('.' + id) );

                        slider.direction = (index > slider.currentSlide) ? 'next' : 'prev';
                        slider.flexAnimate(index, slider.pauseOnAction);
                    });
                },
                before: function(slider){
                    var id = getPostIdClass( slider.slides.eq(slider.animatingTo) );
                    if ( id <= 0 ) return;

                    jQuery('#slider_nav .item').removeClass('current');
                    jQuery('#slider_nav .item.' + id).addClass('current');

                    if ( jQuery('#slider_nav .row').length > 1 ) {
                        var navSlider = jQuery('#slider_nav').data('flexslider'),
                            currPage = navSlider.slides.index( navSlider.slides.find('.item.' + id).parent('.row') );
                        navSlider.direction = (currPage > navSlider.currentSlide) ? 'next' : 'prev';
                        navSlider.flexAnimate(currPage, navSlider.pauseOnAction);
                    }
                }
            });

            jQuery('#slider_nav .item').wrapInChunks('<div class="row" />', 4);

            jQuery('#slider_nav').flexslider({
                selector: '.tiles > .row',
                direction: 'vertical',
                controlNav: true,
                directionNav: false,
                animationLoop: false,
                animation: 'slide',
                useCSS: true,
                smoothHeight: false,
                touch: false,
                slideshow: false,
                pauseOnAction: true,
                animationSpeed: 600
            });
        }<?php

    }

    if ( option::get('carousel_posts_show') == 'on' ) {

        ?>if ( jQuery('#carousel .item').length > 0 ) {
            jQuery('#carousel .item').wrapInChunks('<div class="row" />', 3);
            jQuery('#carousel .row').append('<div class="clear" />');

            if ( jQuery('#carousel .item').length > 3 ) {
                jQuery('#carousel_wrap').append('<div id="carousel_nav"><div class="prev">Prev</div><div class="next">Next</div></div>');

                jQuery('#carousel .row').each(function(){
                    var max = Math.max.apply(null, jQuery('h4.title',this).map(function(){return jQuery(this).height()}).get());
                    jQuery('h4.title', this).css('min-height', max);
                });
                jQuery('#carousel').height(jQuery('#carousel .row').height());

                jQuery('#carousel').serialScroll({
                    axis: 'y',
                    cycle: false,
                    constant: false,
                    force: true,
                    items: '.row',
                    prev: '#carousel_nav .prev',
                    next: '#carousel_nav .next',
                    onBefore: function(e,elem,$pane,$items,pos){
                        $pane.height(jQuery(elem).height());
                    }
                });
            }
        }<?php

    }

}
?>

});
</script>

由于脚本中的 php 部分,我还没有设法添加它。我不知道如何将javascript与php分开并将其以正确的方式添加到footer.php。

任何人?

4

2 回答 2

2

从脚本中删除您的 php 代码并将您的脚本放在一个文件中,假设名称为 custom-script.js

然后在你的主题functions.php文件中添加这个带有wp_enqueue_script的js

function my_scripts_method() {
    if ( is_home() ) {

       if ( option::get('featured_posts_show') == 'on' ) {
        wp_enqueue_script('custom-script',get_stylesheet_directory_uri() . '/js/custom_script.js',  array( 'jquery' ), '', true );
        }
    }
}

add_action( 'wp_enqueue_scripts', 'my_scripts_method' );
于 2013-07-31T15:54:06.800 回答
1

除了 Datta Parad 的回答,使用wp_localize_script()来获取您的动态值。

有关使用指南,请参阅 Codex(上面的链接)和Pippin 的文章

于 2013-07-31T16:31:27.797 回答