Check first parent of page in Wordpress and add body id


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. if ( is_page() && $post->post_parent ) {
  3. $id = get_the_ID();
  4. $a = get_ancestors( $id, 'page' );
  5. $root=count($a)-1;
  6. $rootId = $a[$root];
  7. }
  8. else {
  9. $rootId = get_the_ID();
  10. }
  11. ?>
  12.  
  13.  
  14. <body <?php body_class(); ?> id="root-id-<?php echo $rootId ?>">

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.