du512926 2017-04-03 14:35
浏览 59
已采纳

高级自定义字段wordpress自定义类

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.

  • 写回答

1条回答 默认 最新

  • doushi9376 2017-04-03 16:48
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建