尝试在选定的页面上显示CSS

发布于 2025-02-11 00:45:48 字数 244 浏览 1 评论 0原文

尝试在WordPress中选定的页面中显示某些CSS。我的标题中有以下代码。

<?php 
    if ( is_page('105') ) {?>
        <style>.article-header {display:none;}</style>
<?php   } ?>

仅当我们登录管理部分时,上面的代码工作。当我们没有登录时,该代码不起作用。

trying to display certain css in selected page in wordpress. I have below code in header.

<?php 
    if ( is_page('105') ) {?>
        <style>.article-header {display:none;}</style>
<?php   } ?>

above code work only when we are logged into admin section. The code is not working when we are not logged in. According to me it should work regardless of logging in or out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘年祭陌 2025-02-18 00:45:48

使用操作钩将以下代码添加到functions.php文件中。
如果要在页脚上添加样式或脚本,则可以使用wp_footer挂钩,或者如果要将自定义CSS或脚本添加到标题中,则可以使用wp_head hook。

add_action("wp_head", function() {
  global $post;
  if ($post->ID == 105) {
    ?>
      <style>
        .article-header {display:none;}
      </style>
    <?php
  }
});

is_page()函数有时不起作用。

添加样式后,您将需要清除WordPress的缓存。

谢谢

Add the following code to functions.php file using action Hook.
if you want to add style or script on footer then you can use wp_footer hook or if you want to add custom css or script to header you can use wp_head hook.

add_action("wp_head", function() {
  global $post;
  if ($post->ID == 105) {
    ?>
      <style>
        .article-header {display:none;}
      </style>
    <?php
  }
});

is_page() function sometime not working.

after adding the style you will need to clear the cache of wordpress.

Thanks

悲欢浪云 2025-02-18 00:45:48

注意:最好使用wp_add_inline_style()函数。

  1. 删除'';
    示例=&gt; is_page(105)

  2. 如果未设置当前查询在网站上的现有页面设置当前查询。

  3. 在代码之前检查IS_USER_LOGGED_IN()函数。
    您的代码可能写在有条件的功能中!

https://developer.wordpress.org/reference/functions/wp_add_inline_style/

Note: it's better to use the wp_add_inline_style() function.

  1. Remove '';
    example => is_page(105)

  2. The is_page() function will return false if the current query isn't set up for an existing page on the site.

  3. Check is_user_logged_in() function before codes.
    Your code is probably written inside a conditional function!

https://developer.wordpress.org/reference/functions/wp_add_inline_style/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文