2015-08-15 78 views
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('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 &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(); ?> 

回答

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文件,在這種情況下,你可以在如果跳過只是在頂部位置放置以下行:

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

嗨德米特里,我都嘗試,可惜他們都沒有工作。函數文件夾是我最想要放置代碼的位置,但是當我將第一條建議放在那裏時,它提出了消息 「警告:call_user_func_array()期望參數1是有效的回調函數' lwh_scripts'找不到或函數名無效/var/sites/l/lucieaverill.co.uk/public_html/wp-includes/plugin.php on line 496「 你知道這意味着什麼嗎? 較短的代碼只顯示在頁面頂部。 – Jack1991

+0

我的錯誤,複製和粘貼錯誤,用enqueue_styles代替lwh_scripts – Dmitriy

+0

對於短代碼,用php標籤包裝它: <?php代碼在這裏?> – Dmitriy

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