0

SOLUTION

Big shoutout to the wonderful SallyM, who answered this very succinctly in the comments of this post.

If it helps anybody, here's the code that I used.

Note: Replace the //CONTENT// with your own site content

function include_template_specific_css() {
if (is_page_template('//TEMPLATEFILENAMEWITHDIRECTORY//.php'))
    wp_enqueue_style('//CSSFILENAME//', get_template_directory_uri().'//CSSFILEURL//.css');
}
add_action('wp_enqueue_scripts', 'include_template_specific_css');

Good luck!

/SOLUTION

I'm attempting to call a specific stylesheet within the php of a new post template (one of four).

The stylesheet is 'Featured-Article.css'.
The template file is 'Featured-Article.php'.

Code I've attempted to place within the template includes:

<?php wp_enqueue_style('Featured-Article', get_stylesheet_directory_uri().'assets/css/Featured-Article.css',array('style')); ?>

and

<style> <?php include 'assets/css/Featured-Article.css'; ?> </style>

I'm wondering whether this is even possible at this point, short of meddling with functions.php to create a "if (is_page_template('Featured-Article.php')) etc" - something substantially beyond my ability!

Any ideas?

UPDATE 1:

It looks like the consensus is that such a callback must take place from within a theme's functions.php file. To this end, here's a draft code snippet that I'd hope might accomplish our aim, however sadly didn't work.

function //theme//_include_page_specific_css() {
if (is_page_template('Featured-Article.php'))
    wp_enqueue_style('Featured-Article', get_template_directory_uri(). '/assets/css/Featured-Article.css'); }


add_action('wp_enqueue_style', 'Featured-Article');
    }

Does anyone have any ideas?

UPDATE 2 - SOLVED

Big shoutout to the wonderful SallyM, who answered this very succinctly in the comments of this post.

If it helps anybody, here's the code that I used.

Note: Replace the //CONTENT// with your own site content

function include_template_specific_css() {
if (is_page_template('//TEMPLATEFILENAMEWITHDIRECTORY//.php'))
    wp_enqueue_style('//CSSFILENAME//', get_template_directory_uri().'//CSSFILEURL//.css');
}
add_action('wp_enqueue_scripts', 'include_template_specific_css');

Good luck!

  • 1
    "Meddling" in functions.php with is_page_template() would be the correct way to do this. – Jacob Peattie Jul 17 at 3:07
  • @JacobPeattie I hear it - meddling is a verb that probably overstates my ability to deal with PHP, if anything. Haha. How would you recommend approaching it? A draft I'd been working on looks like this: if ( is_page_template('Featured-Article.php')) { function //themename//_include_page_specific_css() { wp_enqueue_style('Featured-Article', get_template_directory_uri().'assets/css/Featured-Article.css'); } add_action('wp_enqueue_scripts', 'Featured-Article'); } however this doesn't seem to work – Peter Matthews Jul 17 at 3:47
  • 1
    @PeterMatthews The callback name is incorrect: add_action('wp_enqueue_scripts', 'Featured-Article'). The is_page_template() check should also be done from inside the callback. And get_template_directory_uri() doesn't include the ending slash (/), so you should add it manually: get_template_directory_uri() . '/assets/css/Featured-Article.css' – Sally CJ Jul 17 at 6:39
  • 1
    add_action( 'hook name', 'function name' ) - that's the basic syntax of add_action(). So if the function name is neve_include_page_specific_css, then the proper syntax is add_action( 'wp_enqueue_scripts', 'neve_include_page_specific_css' ) – Sally CJ Jul 17 at 12:05
  • 1
    @SallyCJ you are amazing. Thank you so much for your patience, especially regarding the callback syntax which I really should have known already. Made my day, and made this project. Thank you! – Peter Matthews Jul 17 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.