GetSimple Support Forum

Full Version: Get closest parent to root?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I want to make my own extra page class, in addition to get_page_slug(), something like another CMS has, called rootParent--basically it will echo the closest parent to root, so I can add CSS classes section-by-section. Possible? Thanks.
yup
You can add custom functions in function.php file located in your theme directory, and call those functions from within template.
Wiki contains all needed info.
I'd also like to know what the best way is to get the root parent's slug, at the moment I'm just grabbing the document's parent. I'd imagine it uses menu_data().

PHP Code:
<?php // so the syntax is highlighted correctly ?>
<body
    id="d-<?php get_page_slug(); ?>"
    class="p-<?php echo (get_parent(false) != '') ? get_parent(false) : 'orphan' ?>"> 
Insert this at the beginning of your template, or in your theme's functions.php file (create it if it doesn't exist):

PHP Code:
<?php
function get_top_parent() {
  
$slug return_page_slug();
  while (
returnPageField($slug,'parent') != '') {
    
$slug returnPageField($slug,'parent');
  }
  echo 
$slug;
}
?>

Then use it in your template:
PHP Code:
<?php get_top_parent(); ?>

In your example, it could be like this:
PHP Code:
<?php // so that the syntax is highlighted correctly ?>
<body id="d-<?php get_page_slug(); ?>" class="p-<?php get_top_parent(); ?>"> 
Hmm...this would indicate that I was wrong to organize my site with Home as the parent of every page Big Grin
lol
@maruchan

Then, change the while... line to:

PHP Code:
while (!in_array(returnPageField($slug,'parent'),array('','index'))) { 

(assuming that by "Home" you mean the index page)