Forum

Thread tagged as: Question

Set parent page to 'active' when using perch_content_custom

In an effort to streamline the editing process for a site's end-users (differing degrees of computer literacy etc.), I'm using an area with multiple items allowed in conjunction with perch_content_custom to output content for a specific page.

This page (project.php) exists outside of the Perch interface, and pulls in content like so:

    <?php include($_SERVER['DOCUMENT_ROOT'].'/_includes/header.php');?>

    <?php 

        $project = perch_content_custom('Projects', array(
            'page'=>'/work/index.php',
            'template'=>'project.html',
            'skip-template'=>'false',
            'filter'=>'project_slug',
            'match'=>'eq',
            'value'=>$_GET['p'],
            'return-html'=>'true'
        ));

        print($project['html']);

    ?>
<?php include($_SERVER['DOCUMENT_ROOT'].'/_includes/footer.php');?>

The page lives inside the 'work' directory, and its index page is where new items are added. The index page displays a cut-down version of this content, again using perch_content_custom.

I'm using .htaccess rewrite rules to map domain.com/work/project_slug to domain.com/work/project.php?p=project_slug. This works fine.

What I'd like to do is set the 'Work' item in the main navigation as active (as it would be if I was using subpages). Is there a way to do this with Perch, or do I need to set up a different header file with the 'Work' item manually set as active?

Mike Armstrong

Mike Armstrong 0 points

  • 6 years ago

I take it that by "outside the Perch interface" you mean that the project pages aren't in Perch's pages list, right?

In Perch you could add project.php to the Pages list (as a subpage of Work) and set it to hide from the navigation. Then it will be "known" by Perch and can affect the navigation listings/highlighting.

Alternatively, I sometimes employ a simple jQuery solution for such situations so different header files aren't needed. The "special nav cases" code may do the trick if you add in your "/work" case.

// highlight nav by href, adds 'current' and 'ancestor' classes
var pathname = window.location.pathname;
$('a[href="' + pathname + '"]').addClass('current').parents('li').children('a').addClass('ancestor');

// special nav cases
var rootPaths = ['/team','/another/path','/etc'];
var path;
for (var i = rootPaths.length - 1; i >= 0; i--) {
    path = rootPaths[i];
    if (pathname.indexOf(path) === 0) {
        // just affect top nav
        $('#nav a[href^="' + path + '"]').parent().addClass('current').parents('li').addClass('current');
    }
}

Note that the first bit adds the classes to the <a> tags and the second adds them to the <li> tags. Adjust for your needs.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Have you tried telling Perch what you want the page to be?

PerchSystem::set_page('/work/index.php');

Ah, that did it - thanks Drew.