Products
GG网络技术分享 2025-03-18 16:12 5
relatively new to wordpress and ACF,
Basically I have a custom field which is a button that I need to change to a different colour based on what page they are on, so for example
Page 1 - Button needs to be blue
Page 2 - Button needs to be red
But they both use the same ACF button
Is there a way to do this or do I need to create individual ones of each page?
Thanks in advanced.
图片转代码服务由CSDN问答提供
感谢您的意见,我们尽快改进~
功能建议对wordpress和ACF来说相对较新,</ p>
基本上我有一个自定义字段 这是一个按钮,我需要根据它们所在的页面更改为不同的颜色,例如</ p>
Page 1 - 按钮需要为蓝色
第2页 - 按钮需要为红色</ p>
但是它们都使用相同的ACF按钮</ p>
有没有办法做到这一点或我是否需要创建单独的按钮 每个页面?</ p>
先谢谢。</ p>
</ div>
Wordpress adds unique classes to the body element of each page/post - and this can help you achieve what you need.
First inspect the body element of your pages - Page 1 probably has a class page-1
and Page 2 probably has a class page-2
.
Then you can use CSS rules to target your buttons:
/* Target Page 1 Button */.page-1 .btn {
background-color: red;
}
/* Target Page 2 Button */
.page-2 .btn {
background-color: blue;
}
Alternatively, you could use the WordPress is_page()
function to add a unique class to your buttons determined by page:
<?php// e.g. in functions.php
function extraButtonClass() {
// Target by page slug or ID
if(is_page(\'page-1\')) {
return \' red\';
} elseif(is_page(\'page-2\')) {
return \' blue\';
} else {
return null;
}
}
// In template:
<button class=\\\"btn<?php echo extraButtonClass(); ?>\\\">My Button</button>
Demand feedback