Home > Net >  trying to display css on selected page
trying to display css on selected page

Time:06-29

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.

CodePudding user response:

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/

CodePudding user response:

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

  • Related