2012-01-07 77 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