dongwei4103 2014-05-30 13:27
浏览 14

在导航菜单父母和孩子上更改CSS

I have a PHP script that will assign CSS attributes to headings based on which menu item they appear under.

I have this menu structure. About and its children need to have orange headings and Services and its children need to have purple headings

ABOUT SERVICES page1 page 4 page2 page 5 page3 page 6

Currently my scripts will detect whether a page is a child of a certain page but all of the headings are styled with that of the first if statement of my script. (i.e. I check for services first and everything is assigned a purple heading)

Here are the two scripts, could anyone tell me what is going wrong?

if(get_the_title($ID) === "Services" || is_tree($ID) == true) {
     echo '<style type="text/css">
                .header-page h1 {
                        background-color: #9B87A9;
                    }

                    .header-page h2 {
                        background-color: #9B87A9;
                    }
                </style>';
}

else if(get_the_title($ID) === "About" || is_tree($ID) == true) {
    echo '<style type="text/css">
                .header-page h1 {
                        background-color: #dea949;
                    }

                    .header-page h2 {
                        background-color: #dea949;
                    }
                </style>';
}

and the second script/function:

function is_tree($pid) {      // $pid = The ID of the page we're looking for pages underneath
global $post;         // load details about this page
if(is_page()&&($post->post_parent==$pid||is_page($pid))) 
           return true;   // we're at the page or at a sub page
else 
           return false;  // we're elsewhere
};
  • 写回答

1条回答 默认 最新

  • dongqi8863 2014-05-30 14:14
    关注

    You should not echo some CSS inside style tags, but rather conditionally assign classes to your menus or menu items and then use css for the specific classes.

    What you are doing here is assigning the same styles to all .header-page h1 elements (is that the menu title?), which will apply to all <h1 class="header-p[age"> .

    If your ABOUT menu had a distinct class, let's say .about you would just use different css for this element, without needing to use php conditionals.

    Example, with html and css:

    <h1 class="header-page about">About</h1>
    <ul class="menu about">
     ...
    </ul>
    
    
     <h1 class="header-page services">Services</h1>
     <ul class="menu services">
     ...
     </ul>
    

    And now you can apply styles without the conditional:

     <style>
     h1.header-page.about {color:black}
     ul.menu.about {color:red}
     h1.header-page.services {color:green}
     ul.services {color:yellow}
     </style>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值