繁体   English   中英

如何使用条件Wordpress语句将特定代码从页脚传递到特定页面

[英]How can I make use of conditional Wordpress statement to deliver specific code from footer to specific pages

我最近添加了一个新页面,其中包含一些其他页脚代码。 我已经在footer.php中添加了它。 但是,这些代码会出现在我的站点中所有页面上,因为它们位于共同的footer.php文件中。 如何在该代码之前的页脚中添加条件语句,以使该代码仅在我想要的页面中可见? 当此代码出现在所有页面中时,由于代码之间存在一些冲突。

if( is_page(7656))
{
<script>
  $.noConflict();
jQuery(document).ready(function(){

    jQuery(window).scroll(function() {

        var docViewTop = jQuery(window).scrollTop();
        var docViewBottom = docViewTop + jQuery(window).height();
        var customBottom = (jQuery(window).height())/3;
        var viewPort = jQuery(window).height();

        // console.log('viewPort', jQuery(window).height());
        // console.log('customBottom', customBottom);
        // console.log('docViewTop', docViewTop);
        // console.log('docViewBottom', docViewBottom);

        jQuery(".detectActive").each(function() {

            var elemTop = jQuery(this).offset().top;
            var elemBottom = (elemTop + jQuery(this).height() );
            var difference = docViewBottom - elemBottom;

            // console.log('elemTop', elemTop);
            // console.log('elemBottom', elemBottom);
            // console.log('difference', difference);
            if(( difference >= customBottom) && (difference <= viewPort)) {

                console.log('show now');                
                let active = jQuery(this).attr("data-active");
                jQuery('.commonLink').removeClass("hover_class");
                jQuery('#'+active+'-nav').addClass("hover_class");
                console.log('visible', active);  
            }

        });
    });

});
</script>
}

我已经尝试了上面的代码,但是,上面的代码显示在每个页面上。 如何在页面ID为7656的页面上显示?

这是你的代码

添加更多页面ID以数组用逗号分隔它们<?php if ( is_page( array( 7656, 7655, 7651 )) ) ) : ?>

<?php if ( is_page( array( 7656 ) ) ) : ?>
<script>
  $.noConflict();
jQuery(document).ready(function(){

    jQuery(window).scroll(function() {

        var docViewTop = jQuery(window).scrollTop();
        var docViewBottom = docViewTop + jQuery(window).height();
        var customBottom = (jQuery(window).height())/3;
        var viewPort = jQuery(window).height();

        // console.log('viewPort', jQuery(window).height());
        // console.log('customBottom', customBottom);
        // console.log('docViewTop', docViewTop);
        // console.log('docViewBottom', docViewBottom);

        jQuery(".detectActive").each(function() {

            var elemTop = jQuery(this).offset().top;
            var elemBottom = (elemTop + jQuery(this).height() );
            var difference = docViewBottom - elemBottom;

            // console.log('elemTop', elemTop);
            // console.log('elemBottom', elemBottom);
            // console.log('difference', difference);
            if(( difference >= customBottom) && (difference <= viewPort)) {

                console.log('show now');                
                let active = jQuery(this).attr("data-active");
                jQuery('.commonLink').removeClass("hover_class");
                jQuery('#'+active+'-nav').addClass("hover_class");
                console.log('visible', active);  
            }

        });
    });

});
</script>
<?php endif; ?>

即使您的问题是关于WordPress和PHP条件的使用,您也可以解决客户端问题。 WordPress使用您当前正在观看的页面或帖子的ID将一个类添加到body标签,因此您所要做的就是添加if($('body')。hasClass('page-id-7656'))到您的代码,如下所示:

<script>
$.noConflict();
jQuery(document).ready(function(){
    if ($('body').hasClass('page-id-7656')) {
        // The rest of your code goes here.
    }
});
</script>

但是,如果您需要纯PHP解决方案,则@ andrey-shandrov的答案是正确的方法。

暂无
暂无

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

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