Support » Fixing WordPress » Add Body ID to Control Page Elements

  • Hey, I’m trying to add a Body ID to my different pages, the following works fine for all the parent pages:

    <body id="<?php echo $post->post_name; ?>">

    but for any sub-pages it pulls the name of the sub-page. How could I change this to only use the parent page slug as the id?

Viewing 2 replies - 1 through 2 (of 2 total)
  • i’m trying out this way now:

    <?php
    		$page = $_SERVER['REQUEST_URI'];
    		$page = str_replace("/","",$page);
    		$page = str_replace(".php","",$page);
    		$page = str_replace("?s=","",$page);
    		$page = $page ? $page : 'default'
    	?>
    
    	<body id="<?php echo $page ?>">

    but am running into a similar problem with subpages of a parent page and not sure how to fix it.

    ah, yanked this from the sandbox theme. seems to work…? wondering if it can be simplified for my purposes?

    <?php
    		  $current_page = $post->ID;
    		  $parent = 1;
    		  while($parent) {
    		  $page_query = $wpdb->get_row("SELECT post_name, post_parent FROM $wpdb->posts WHERE ID = '$current_page'");
    		  $parent = $current_page = $page_query->post_parent;
    		  if(!$parent)
    		  $parent_name = $page_query->post_name;
          }
          ?>
          <body id="<?php echo (is_page()) ? "$parent_name" : ((is_home()) ? "blog" : ((is_search()) ? "other" : ((is_single()) ? "blog" : "blog"))); ?>">

    Since I’m mainly going to be using this as a CMS I probably need only the parent name ones and the other ones can just be called other so it seems like there’d be a simpler way to list out the body tag.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Add Body ID to Control Page Elements’ is closed to new replies.