Multiple 'if' variables - COUCH CMS - PHP Menu

Hello, I am trying to incorporate my HTML menu into the CouchCMS content management system.

I need to set an active class on the parent

  • when any of the child pages are selected.

    Here is my working code:

    <li
    <cms:if k_template_name=='index.php'>class="active"</cms:if> >
    <a href="<cms:link 'index.php' />" ><img class="homeIcon" src="../images/home_icon.png" width="20px">Self-Drive&nbsp;Hire</a>
    
  • SubPage
  • SubPage
  • SubPage
  • The above adds the ‘Active’ class to the parent

  • when the matching page is selected.

    However, I would also like to add the class “Active” to the parent

  • if the sub-menus are selected.

    So in effect, add the “Active” class
    if
    k_template_name== index.php or tesla_home.php or telsa_x_home.php

    I tried the following:
    <cms:if k_template_name==‘index.php’ || ‘telsa_home.php’ || ‘tesla_x_home.php’ || ‘bmw_home.php’ || ‘renault_home.php’>class=“active”</cms:if>>

    But as expected this did not work. I presume I should be using an Array - but all my attempts have failed and I am struggling to get my head around it.

    Any help would be greatly appreciated.

    Regards
    Steve

  • Well, I do not know this platform, but you normally have more than one page.
    Is the code you showed us above in an “include” file? Or, is it just on the index.php page itself?

    Normally there are two ways to handle these types of cms pages in platforms. Either the template has a
    header file that handles it all, or each page handles it’s own. I am guessing this code above is placed in a
    file that contains all of your header and menu items. To fix that, you would need to know which current page
    you are currently on. In HTML, there is no easy way to do that, you would need to repeat the above code
    for each page that displays this header file. Then, it should work. Otherwise, you have to use simple
    PHP to do that… To do it in PHP, you would do it something loosely like this:
    <li <?PHP if (basename($_SERVER['PHP_SELF'])=="index.php) { echo ' class="active" '; } >
    Again, you would add this code to each of the pages you access with this header code…

    Hi ErnieAlex thanks for your reply.

    Was there meant to be some sample code in your reply? it looked like it but I wasn’t sure.

    You are correct this code is an include file (containing only the menu).

    This CMS uses individual templates for each page, so effectively each page has its own ID, so when loaded the CMS knows what page is being displayed. “k_template_name”

    The code provided is working as intended, (adding a class to the selected menu item) however it does not account for nested (sub) pages.

    If a sub page is selected, I need to add the “active” class to the parent

  • Current Code:

    <li
    <cms:if k_template_name=='index.php'>class="active"</cms:if> >
    <a href="<cms:link 'index.php' />" ><img class="homeIcon" src="../images/home_icon.png" width="20px">Self-Drive&nbsp;Hire</a>
    

    So if this adds the class to the selected li:

    <li <cms:if k_template_name=='pagename.php'> class="active"</cms:if> >

    How would I make it add the class to the parent li instead?

    <li <cms:if k_template_name=='pagename.php'> item-parent class="active"</cms:if> >

    Thank you for your patience.

    Kind Regards

    Steve

  • Hello, thanks again for your previous reply, I have been provided with the corrected code to resolve my issue, I was not writing the CouchCMS / PHP markup correctly.

    This is the correct code for what I was trying to achive:

    <li
    <cms:if k_template_name=='index.php' || k_template_name=='telsa_home.php' || k_template_name=='tesla_x_home.php' || k_template_name=='bmw_home.php' || k_template_name=='renault_home.php'>class="active"</cms:if>
    >
    

    Have a great day.

    Steve

    Yes, Steve, my post was “preformatted” but was marked as a quote so it did not show.
    I editted it so it would…

    But, glad you solved it on your own. Always a nice feeling!

    Sponsor our Newsletter | Privacy Policy | Terms of Service