WordPress.org

Forums

Add Body ID to Control Page Elements (3 posts)

  1. wyclef
    Member
    Posted 6 years ago #

    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?

  2. wyclef
    Member
    Posted 6 years ago #

    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.

  3. wyclef
    Member
    Posted 6 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.