Forums

Setting Body ID dynamically in Ajaxed Site (1 post)

  1. incubus820
    Member
    Posted 4 months ago #

    I have searched everywhere and I can't find a way to dynamically set a body ID on an ajaxed site. Right now I am using the jquery hashchange plugin which allows access to the back button and deep linking, but it places a # symbol in the URL so it throws off the typical php or WP function for setting a body ID. Does anyone know how I can set a dynamic Body ID based on the page slug. It also needs to take into consideration the # symbol in the URL. This will allow me to target specific CSS elements in the ajaxed main content based on the specific pages.

    Example: http://www.sitename.com/#/photos/

    I would like to set the body ID to "photos".

    I have tried the following, but they either leave the ID empty, set it to "home" or "default".

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

    2. <?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 ?>">

    3. <?php
    $url = explode('/', $_SERVER['REQUEST_URI']);
    $dir = $url[1] ? $url[1] : 'home';
    ?>
    <body id="<?php echo $dir ?>">

    Any help would be greatly appreciated!

Reply

You must log in to post.

About this Topic