Forums

[resolved] Linking to custom CSS file (5 posts)

  1. timsig
    Member
    Posted 1 year ago #

    Hello,
    I have two pages that I want to link to a custom stylesheet. The customer care page is based on a template, and the single-5.php is a custom single post page. The first links perfectly, the second not at all. I can't understand why, as the code is almost identical:

    <?php if (is_page_template('customerCare_template.php')) {?>
        <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/customerCare.css">
        <?php }?>
    
        <?php if (is_page('single-5.php')) {?>
        <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/customerCare.css">
        <?php }?>

    Thanks for any help.

  2. JPry
    Member
    Posted 1 year ago #

    Check out the is_page() function reference. That function isn't looking for a php file name. It's looking for an ID, a slug, a title, or an array of any of those. So if your page has an ID of 5, you'd want to use is_page( 5 );.

  3. timsig
    Member
    Posted 1 year ago #

    Thanks for the reply; since this is not a page listed in the dashboard, but a php file called by the theme, I found the easiest way round it was to link to the stylesheet from the file itself, rather than from the header.

  4. JPry
    Member
    Posted 1 year ago #

    In case you didn't already know, you can tie into the header code from (just about) anywhere by adding an action. Your code in the "single-5.php" file would be this:

    <?php
    add_action( 'wp-head', 'single-5-style' );
    function single-5-style { ?>
       <link rel="stylesheet" href="<?php bloginfo( 'template_url' ); ?>/css/customerCare.css">
       <?php
    }
  5. timsig
    Member
    Posted 1 year ago #

    Thanks for that; I love a learning curve.

Topic Closed

This topic has been closed to new replies.

About this Topic