Forums

Template Tags: Individual id names for Pages (5 posts)

  1. christyyyjoy
    Member
    Posted 3 years ago #

    Hello!

    My client wants several static pages in addition to his blog, to be navigated throgh a nav bar at the top. I'd like each of these pages to have a different <body id=""> so that the id can cascade down to where I have a class set up for the "active" page to be indicated in this nav bar. I was trying to use the_permalink and wp_title, but each of those are long and use characters I can't have in my CSS. Ideally, I'd just want the page ID number to be placed there, but the_ID can only be in the loop.

    Is there a way to do this, or should I just create a separate template for each page? Thanks!

    Christy

  2. MichaelH
    Volunteer
    Posted 3 years ago #

    Like:

    if (is_page( )) {
      $page = get_query_var('page_id');
    }
  3. christyyyjoy
    Member
    Posted 3 years ago #

    A solution like that would work, I guess i was just looking for something a bit simpler.

    Is there not a single token to represent a Page's ID outside of the loop?

    Or, is there a way to use wp_title to return the page title without separators around it? That may not completely work, though, because my pages have phrases for titles, and I can't assign a phrase as a selector.

    Thanks for your insight! =)

  4. christyyyjoy
    Member
    Posted 3 years ago #

    To simplify the question without any examples necessary:

    Is there a single template tag that I can use outside of the loop that will return the Title or the ID of a Page?

  5. davesgonebananas
    Member
    Posted 3 years ago #

    I don't know of a template tag but if you are using permalinks like /about, /contact and such then you could use some regular php.

    <?php
    if ( is_page() ) :
    	$page = next(explode('/', $_SERVER['REQUEST_URI']));
    else :
    	$page = "blog";
    endif;
    ?>

    <body class="<?php echo $page ?>">

Topic Closed

This topic has been closed to new replies.

About this Topic