简体   繁体   English

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

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

I have recently added a new page which has some additional footer code. 我最近添加了一个新页面,其中包含一些其他页脚代码。 I have added it in footer.php. 我已经在footer.php中添加了它。 However, those codes appear for all the pages in my site as it's in common footer.php file. 但是,这些代码会出现在我的站点中所有页面上,因为它们位于共同的footer.php文件中。 How can I add a conditional statement in my footer before that code which will make that code to be visible only in the page that I want? 如何在该代码之前的页脚中添加条件语句,以使该代码仅在我想要的页面中可见? As there is some conflicts between code when this code appear in all the pages. 当此代码出现在所有页面中时,由于代码之间存在一些冲突。

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>
}

I have tried the above code and still, the above code shows on every page. 我已经尝试了上面的代码,但是,上面的代码显示在每个页面上。 What can I do to show on page with a page id of 7656? 如何在页面ID为7656的页面上显示?

Here is your code 这是你的代码

Add more page ids to array divide them with comas <?php if ( is_page( array( 7656, 7655, 7651 )) ) ) : ?> 添加更多页面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; ?>

Even though your question is about WordPress and the use of a PHP condition, you can also solve the problem client-side. 即使您的问题是关于WordPress和PHP条件的使用,您也可以解决客户端问题。 WordPress adds a class to the body tag with the ID of the page or post you're currently watching, so all you need to do is add if ($('body').hasClass('page-id-7656')) to your code, like this: 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>

If, however, you want a pure-PHP solution, @andrey-shandrov's answer is the way to go. 但是,如果您需要纯PHP解决方案,则@ andrey-shandrov的答案是正确的方法。

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

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