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

时间:2015-08-15 15:06:09

标签: php css wordpress stylesheet

如果有人能帮助我,我真的很感激。 我知道这是一个经常被问到的问题,但是我花了好几天时间查看其他人的帖子,试图找出如何使用我的函数文件夹将自定义样式表链接到我正在处理的页面模板。

到目前为止,我发现看起来最有可能工作的代码就是这个 -

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');
}

我尝试创建的网页模板被称为" work",因为我已经尝试添加到代码中,但我正在做的事情不是'是的,因为它要么使用主样式表,要么它没有任何效果,留下我的页眉和页脚之间的空白页。

您可以在下面看到我的页面模板php的代码。 (我对wordpress和编码很新,所以如果我犯了任何愚蠢的错误,请告诉我)。我已经开发了我从头开始使用的主题。

<?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(); ?>

2 个答案:

答案 0 :(得分:0)

假设您的工作模板位于主题文件夹的根目录中并且实际上称为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');

显然会进入你的functions.php文件。您也可以将它直接放在您的work.php文件中,在这种情况下,您可以跳过if并将以下行放在顶部:

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

答案 1 :(得分:0)

将样式排入队列时,需要使用用于注册它的句柄名称而不是文件名。在这种情况下,您将样式注册为“工作方式”,因此您应该使用该名称将其排入队列,如下所示:

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');
}