WordPress.org

Ready to get started?Download WordPress

Forums

Dynamically changing body id for pages? (8 posts)

  1. jlivingston
    Member
    Posted 9 years ago #

    I'm stumped and was wondering if anyone knew how to dynamically change the body id so different background images could be used depending on the page you were at. Call me crazy but I have a site that needs a specific but different look (background image) for each page (using wp 1.5). I have a good grasp of CSS but not PHP.

    Is this possible? Any help would be appreciated.

  2. Kafkaesqui

    Posted 9 years ago #

    When you say page, do you mean as in Pages? If so, you might use this method to provide the Page 'slug':

    <body id="<?php echo get_query_var('name'); ?>">

    So if the name of the Page is "About Us", the slug would be "about-us" and so now would the id.

    If you use default permalinks (i.e. /?paged_id=100 ) for the site, use something like this:

    <body id="page-<?php echo get_query_var('page_id'); ?>">

    So you'd get id="page-100".

    Note you'll want to set up a separate page.php template for your theme (if you don't have one already) and use the above in that.

  3. jlivingston
    Member
    Posted 9 years ago #

    Thanks for the reply, that's what I was looking for. Actually they would be for Pages and sub-Pages. What would the code look like if you were trying to provide a unique id for each sub-page? (I'm not using the default permalinks).

    For example I'm creating a site that will have a "Books" page and then under that there will be "Book A", "Book B", etc. I will need a unique background for "Books" (the main page), then "Book A" (a subpage), and "Book B" (another subpage).

  4. richardlee
    Member
    Posted 9 years ago #

    @ jlivingston:

    Book A = book-a
    Book B = book-b

    The page-slug reflects the page title you gave it all lowercase and spaces replaced with '-' (dash). There's no breadcrumbing for subpages if thats what your thinking ;).

    @ Kafkaesqui:

    "Note you'll want to set up a separate page.php template for your theme (if you don't have one already) and use the above in that."

    Please tell me how we go about this Kaf, since the body tag is in header.php, not the Page.php template.. :(

    @ jlivingston: J let me know if you have any clues on how to alter the Page.php and Header.php to add this body id functionality

    PS J you have to rememer for each new page you add you'll have to update you .htaccess file used for permalinks

    Thanks guys

  5. richardlee
    Member
    Posted 9 years ago #

    Hi again,

    Instead of messing around with making dramatic changes to files and making a new Page teamplate file I just add a coniditional statement within Header.php

    <body id="<?php echo (is_page()) ? get_query_var('name') : ((is_home()) ? "home" : ((is_single()) ? "single": ((is_category()) ? single_cat_title() : ((is_archive()) ? "archive" : "")))); ?>">

    Yes I know its complex because I have used the short-hand method for writing conditions however here's what it does;

    • If page is a Page grab slug for id
    • Else if page is home just hardcode id "home"
    • Else if page is a Single post just hardcode id "single"
    • Else if page is Category grab category name as id
    • Else if page is Archive just hardcode id "archive"
    • Else dont know what it is so set id as nothing

    Assumptions: Only Pages and Categories will have a unqiue style per page, other pages will just have a generic style for that type of page.

    Hope this helps ;)

  6. Phoat
    Member
    Posted 9 years ago #

    richardlee: Brilliant... Exactly what I wanted... well sorta.

    Here's what I'm thinking... If the page is a top level page... we could just use the Page Slug for the id... Is it possible tho, to use the Top Level parent's page slug in the body id of a sub-page?

  7. mrsugar
    Member
    Posted 8 years ago #

    Did anyone ever find an answer for this? I have been working in topic http://wordpress.org/support/topic/38264 which seems to have a solution. My last post in that topic has the problem I am running into. I can't seem to get the idea to work, I am not sure how to lay it out in my code.

  8. Joshua Sigar
    Member
    Posted 8 years ago #

    The solution for the issue for this thread is in the 2 posts above yours.

    The code goes, usualy, to header.php where the opening tag for <body> is

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.