【问题标题】:Wordpress filtering add_action customization by pageWordpress 按页面过滤 add_action 自定义
【发布时间】:2021-12-29 04:18:15
【问题描述】:
add_action( 'wp_head', 'betterdocs_customize_css');

如何让它在我指定的页面上运行,而不是在所有内部页面上运行? 由于我将它添加到标题部分,css 出现在所有不必要的页面上。我只希望它在一页上,我该如何过滤它? 例如;只需将其添加到 domain.com/documents 页面的标题中即可。

【问题讨论】:

    标签: php html css wordpress


    【解决方案1】:

    你可以这样添加,

    add_action('wp_head','betterdocs_customize_css');
    function betterdocs_customize_css(){
      //check for the current page is your targeted page by id
      if( is_page( 42 ) ){
         echo "<style> your style here </style>";
      }
    }
    

    通过这样做,您可以在标题中仅打印目标页面的样式。

    【讨论】:

    • 当我们要添加多个页码时怎么办?
    • 将页面ID存储在数组中并使用in_array()函数进行检查
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 2018-02-13
    • 2019-04-07
    相关资源
    最近更新 更多