如果有人可以帮助我,我将非常感激。我知道这是一个经常被问到的问题,但是我花了几天时间浏览其他人的帖子,试图弄清楚如何使用我的functions文件夹将自定义样式表链接到我正在处理的页面模板。
到目前为止,我发现最有可能起作用的代码是此代码-
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');
}
我尝试创建的页面模板被称为“工作”,因为我尝试将其添加到代码中,但是我正在做的事情是不对的,因为它使用主样式表或没有任何作用,在页眉和页脚之间留有空白页。
您可以在下面查看我的页面模板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 »', '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(); ?>
假设您的工作模板位于主题文件夹的根目录中,并且实际上称为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' );
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句