简体   繁体   English

如何将自定义样式表链接到页面模板?

[英]How to link a custom stylesheet to a page template?

If anyone could help me with this I'd be really grateful. 如果有人可以帮助我,我将非常感激。 I know that this is a question that's been asked very frequently, however I've spent days looking at other people's posts trying to work out how to use my functions folder to link a custom stylesheet to a page template I'm working on. 我知道这是一个经常被问到的问题,但是我花了几天时间浏览其他人的帖子,试图找出如何使用我的functions文件夹将自定义样式表链接到我正在处理的页面模板。

So far, the code I've found that looks the most likely to work is this one - 到目前为止,我发现最有可能起作用的代码是此代码-

if( is_page_template( 'work.php' ) ) {
wp_register_style("work-style", get_template_directory_uri() . 
"/css/page-template.css", '', '1.0.0');
wp_enqueue_style('page-template');
}

The page template I'm trying to create is called "work", as I've tried to add to the code, however something I'm doing isn't right, as it either uses the main stylesheet or it has no effect whatsoever, leaving me with a blank page between my header and footer. 我尝试创建的页面模板被称为“工作”,因为我尝试将其添加到代码中,但是我正在做的事情是不对的,因为它使用主样式表或没有任何作用,在页眉和页脚之间留有空白页。

You can see the code for my page template php below. 您可以在下面查看我的页面模板php的代码。 (I'm very new to wordpress and coding in general, so if I've made any stupid errors, please let me know). (我一般对Wordpress和编码还很陌生,因此,如果我犯了任何愚蠢的错误,请告诉我)。 I've developed the theme I'm using from scratch. 我已经从头开始开发了我正在使用的主题。

<?php
/*
Template Name:work
*/
?>

<link href="style.css" rel="stylesheet" type="text/css">

<link href="<?php bloginfo('stylesheet_url'); ?>" rel="stylesheet"  
type="text/css"> 

<body>

<?php get_header(); ?>

<div class="content" id= "workcontent">

<?php if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); ?>

<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">

<div class="entry">
<?php the_content(__('Read the rest of this entry &raquo;', 'kubrick')); ?>
</div>

</div>

<?php endwhile; ?>

<?php endif; ?>

<div id="wrapper">      

<a href="/landscapes"><div class="albums" id= "album1"></div></a>
<a href="/seascapes"><div class="albums" id= "album2"></div></a>
<a href="/macro"><div class="albums" id= "album3"></div></a>
<a href="/cities"><div class="albums" id= "album4"></div></a>
<a href="/long-exposure"><div class="albums" id= "album5"></div></a>
<a href="/miscellaneous"><div class="albums" id= "album6"></div></a>

</div>
</div>

</body>

<?php get_footer(); ?>

Assuming that your work template is in the root of the theme folder and it is actually called work.php, as well as the fact that your css is in the css subfolder and is called template.css, you may want to modify your code as follows: 假设您的工作模板位于主题文件夹的根目录中,并且实际上称为work.php,并且您的css位于css子文件夹中并且称为template.css,那么您可能希望将代码修改为如下:

function enqueue_styles() { 
    if( is_page_template( 'work.php' ) ) {
        wp_enqueue_style('work-style', get_bloginfo('template_url').'/css/page-template.css', true, '1.0.0', 'all' );
    }
}
add_action('wp_enqueue_scripts', 'enqueue_styles');

obviously that goes into your functions.php file. 显然,这进入了您的functions.php文件。 You can also place it directly in your work.php file, in which case you can skip the if and just place the following line at the top: 您也可以将其直接放置在work.php文件中,在这种情况下,可以跳过if并将仅以下行放在顶部:

wp_enqueue_style('work-style', get_bloginfo('template_url').'/css/page-template.css', true, '1.0.0', 'all' );

When enqueuing a style, you need to use the handle name you used to register it rather than the file name. 排入样式时,您需要使用用于注册样式的句柄名称,而不是文件名。 In this case, you register the style as "work-style", so you should use that name to enqueue it, like this: 在这种情况下,您将样式注册为“工作样式”,因此应使用该名称将其加入队列,如下所示:

if( is_page_template( 'work.php' ) ) {
    wp_register_style('work-style', get_template_directory_uri() . 
    '/css/page-template.css', '', '1.0.0');
    wp_enqueue_style('work-style');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM