最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is it possible to link an external stylesheet, exclusively within a specific single post template?

programmeradmin0浏览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!

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论