网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

PHP - 仅在主页上隐藏元素 - WordPress站点

GG网络技术分享 2025-03-18 16:12 4


问题描述:

In my footer.php file, I have this bit of code towards the end of the file:

<?php wp_footer(); ?> 

<div class=\"website-by\">

<br>

Website by <a href=\"https://gatewaywebdesign.com/\" target=\"blank\">Gateway Web Design</a>

</div>

</div>

</body>

As you can see, I\'ve created a \"website by\" link, which links to another website when clicked on.

But I don\'t want this text, or the div that contains it, to appear at all on the home page of the site:

https://thehamburgercollection.com/

I\'ve checked other stackoverflow articles that suggest doing so with JQuery, which I\'ve added to my scripts.js file:

//hide link on the home page

document.ready(function() {

if (window.location.href.indexOf(\'https://thehamburgercollection.com/\')) {

//Hide the element.

jQuery(\'.website-by\').hide();

}

});

And I\'ve also tried hiding the div on the home page with CSS that was suggested here:

.home .website-by {

display: none;

}

But unfortunately neither of these methods have worked. I\'ve declared my class as website-by in the footer.php file, and have added the JQuery that should target that class in the scripts.js file, and have also added the suggested CSS. Any idea why either the JQuery or CSS code isn\'t working?

This is a WordPress website and a custom there. Any help / suggestions / education is appreciated!

网友观点:

I\'m not a WordPress guru, but have you tried doing an if condition in the footer.php? if I got your question right, you need the .website-by to not appear elsewhere but the home page. Maybe something like

<?php wp_footer(); ?> 

<?php if (!is_home()): ?>

<div class=\\\"website-by\\\">

<br>

Website by <a href=\\\"https://gatewaywebdesign.com/\\\" target=\\\"blank\\\">Gateway Web Design</a>

</div>

<?php endif; ?>

</div>

</body>

标签:

提交需求或反馈

Demand feedback