3

相信我,我已经连续几天寻找这个问题的答案。我相信答案很简单,但我无法让它发挥作用。我会尽量具体。

我已经学会了如何为特定的 WordPress 页面设置 CSS,但是我需要为 WordPress 父页面及其子页面设置 CSS。虽然可以通过将每一页都包含到我的代码中来完成,但我知道必须有一种更有效的方法,因为我在每个父页面下有大约 20 页。

我正在寻找的一个例子可以在这里找到:(原始网站) www.viewmonthealth.com

我试图做到这一点的地方:www.notthemama.net/prime/

每个主链接都指向具有单独 css 的不同部分。

这是我所拥有的:

<?php if (is_page('imaging')) { ?>
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/viewmont-imaging.css" type="text/css">
<?php } elseif (is_page('labs')) { ?>
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/viewmont-labs.css" type="text/css">
<?php } else { ?>
    <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" />
<?php } ?>

这非常适用于页面。我只需要包含父页面(我现在拥有的)和子页面(imaging/forms.php)。我怎样才能做到这一点?任何帮助表示赞赏!:D

4

2 回答 2

0

您可以尝试使用父 ID 做一些事情

function is_child($pageID) { 
    global $post; 
    if( is_page() && ($post->post_parent==$pageID) ) {
               return true;
    } else { 
               return false; 
    }
}

把它放在你的函数中,然后

<?php if (is_child(12)) { ?>
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/viewmont-imaging.css" type="text/css">
<?php } elseif (is_child(14)) { ?>
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/viewmont-labs.css" type="text/css">
<?php } else { ?>
    <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" />
<?php } ?>

希望这就是你要找的。:)

于 2013-02-04T05:55:34.547 回答
0

更精简的方法是使用以下条件检查,实际上不需要自定义函数:

if ( $post->post_parent == 2 ) { ?>
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/viewmont-imaging.css" type="text/css">
<?php } ?>
于 2015-11-07T14:58:45.423 回答