2012-01-07 79 views
1

我正在設計一個主題,每個頁面都有不同的文字,背景和其他元素的顏色。我能夠與這些樣式的每一個頁面(及相關崗位類別):子頁面如何在Wordpress上繼承父級的樣式?

<?php if (is_home() || is_search() || is_archive()) 
    { 
    ?> 
    <link rel="stylesheet" href="<?php bloginfo('template_url')?>/css/home.css" type="text/css" media="screen" /> 
    <?php } elseif(is_category('Turismo a Bra') || is_page('Turismo a Bra')) 
    { 
    ?> 
    <link rel="stylesheet" href="<?php bloginfo('template_url')?>/css/turismo-a-bra.css" type="text/css" media="screen" />  
    <?php } elseif (is_category ('Eventi') || is_page('Eventi')) 
    { 
    ?> 
    <link rel="stylesheet" href="<?php bloginfo('template_url')?>/css/eventi.css" type="text/css" media="screen" /> 
    <?php } elseif (is_category ('Arte e Cultura') || is_page('Arte e Cultura')) 
    { 
    ?> 
    <link rel="stylesheet" href="<?php bloginfo('template_url')?>/css/arte-e-cultura.css" type="text/css" media="screen" /> 
    <?php } elseif (is_category ('Enogastronomia')|| is_page('Enogastronomia')) 
    { 
    ?> 
    <link rel="stylesheet" href="<?php bloginfo('template_url')?>/css/enogastronomia.css" type="text/css" media="screen" /> 
<?php } elseif (is_category ('Natura')|| is_page('Natura')) 
    { 
    ?> 
    <link rel="stylesheet" href="<?php bloginfo('template_url')?>/css/natura.css" type="text/css" media="screen" /> 
    <?php } else { ?> 

    <?php } ?> 

問題是當我(和我有很多)子頁面。我希望他們成爲他們的父母。我雖然WP有is_sub_page(#),但沒有運氣。

你知道我應該添加什麼條件來使標題理解何時處理子頁面,並且在這種情況下獲取父標識並基於該頁面的樣式。

我是一個PHP和wordpress的新手,它在我的頭腦中是有道理的,但我不知道如何去描述它。

非常感謝,一個例子是here(子頁都在右上側。

回答

1

要檢查文章是否用某一類別或網頁標題的網頁decends那麼你可以得到其母公司和檢查如:

in_category('Turismo a Bra', $post->post_parent) 

正如你已經有很多的代碼,你這樣做是多次它可能是最好的一個函數內封裝整個檢查:

function needs_style($style, $the_post){ 
    $needs_style = false; 
    //check details of this post first 
    if($the_post->post_title == $style){  //does the same as in_page() 
     $needs_style = true; 
    } 
    elseif(in_category($style, $the_post)){ 
     $needs_style = true; 
    } 
    //otherwise check parent if post has one - this is done recursively 
    elseif($the_post->post_parent){ 
     $the_parent = get_post($the_post->post_parent); 
     $needs_style = needs_style($style, $the_parent); 
    } 
    return $needs_style; 
} 

所以你的代碼看起來像這樣:

if (is_home() || is_search() || is_archive()) { 
    //set stylesheet 
} 
elseif(needs_style('Turismo a Bra', $post)) { 
    //set stylesheet 
} 
elseif(needs_style('Eventi', $post)) { 
    //set stylesheet 
} 
+1

DUDE!有用!非常感謝! – 2012-01-08 15:06:22