技术控

    今日:19| 主题:49249
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Lots of ways to add an ID to the `body` element

[复制链接]
Amrice做作 发表于 2016-10-11 22:29:56
75 1

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
The following is a guest post by    Trishah Woolley. Over the years, Trishah has collected a ton of code snippets that to the job of adding IDs and classes based on some variable information, like the URL or data from WordPress. Each of these snippets were either either collected from documentation or a similar online resource, or written by Trishah based on standard features of the language and API's involved.  
  Over the time I have been doing WordPress web design, there are occasions when I have needed to add an ID or class to the    <body>tag. For example: This often happens when integrating a 3rd party into WordPress that creates its own pages that WordPress doesn't recognize. Or I need more nuanced classes for the site design I'm creating.  
  To change the look of these pages in a way that the styles are scoped just to that page, we need a way to target just that page or category of pages. Below are some of the ways I have found helpful to add IDs and classes to the    <body>of both non-WordPress (but still PHP) and WordPress pages.  
  PHP Variations

  #01 - Add ID from the URL

  This will strip the forward-slash, `.php`, and the `?pg=`, from the URL and use the rest as an ID. Change the array to match your particular situation.
  1. <?php
  2.   $page = str_replace(array('/', '.php', '?pg='), '', $_SERVER['REQUEST_URI']);
  3.   $page = $page ? $page : 'default';
  4. ?>

  5. <body id="<?php echo $page ?>">
复制代码
Example:
  1. http://domain.com/directory/test.php?pg=12345
复制代码
Becomes:
  1. <body id="directorytest12345">
复制代码
#02 - Add ID and Class from URL

  This looks for the `?` in the URL and turns what comes before it into an ID and what comes after it into a class.
  1. <?php
  2.   list($page, $class) = explode('?', $_SERVER['REQUEST_URI']);
  3.   $page = str_replace(array('/', '.php'), '', $page);
  4.   $class = str_replace(array('='), '', $class);
  5. ?>

  6. <body id="<?php echo $page ?>" class="<?php echo $class ?>">
复制代码
Example:
  1. http://domain.com/directory/test/?pg=12345
复制代码
Becomes:
  1. <body id="directorytest" class="12345">
复制代码
#03 - Add ID from file name

  This removes the `.php` and uses the file name as the ID:
  1. <body id="<?=basename($_SERVER['PHP_SELF'],'.php')?>">
复制代码
Example:
  1. http://domain.com/directory/test.php
复制代码
Becomes:
  1. <body id="test">
复制代码
#04 - Add ID from First Level Directory

  This will grab the first level directory and use it as the ID.
  1. $url = explode('/', $_SERVER['REQUEST_URI']);
  2.   $dir = $url[1] ? $url[1] : 'home';
  3. ?>

  4. <body id="<?php echo $dir ?>">
复制代码
Example:
  1. http://domain.com/directory/test.php?pg=123450
复制代码
Becomes:
  1. http://domain.com/directory/test.php?pg=123451
复制代码
WordPress Variations

  WordPress has a native function for applying classes to the body:
  1. http://domain.com/directory/test.php?pg=123452
复制代码
Which results in output like this, depending on what page you are on:
  1. http://domain.com/directory/test.php?pg=123453
复制代码
But you can still do more, like combine it with techniques from above and add classes of your own.
  #05 - WordPress: Add a Class to Default Body Class

  Add your own class to the ones generated by WordPress with this:
  1. http://domain.com/directory/test.php?pg=123454
复制代码
Becomes:
  1. http://domain.com/directory/test.php?pg=123455
复制代码
#06 - WordPress: Add Multiple Classes to Default Body Class

  You can add multiple classes like this:
  1. http://domain.com/directory/test.php?pg=123456
复制代码
Becomes:
  1. http://domain.com/directory/test.php?pg=123457
复制代码
#07 - WordPress: Add ID and Class to Body Tags

  This just may be my favorite! It does the same as the one above but adds the new class to the WordPress'    body_class:  
  1. http://domain.com/directory/test.php?pg=123458
复制代码
Example:
  1. http://domain.com/directory/test/?pg=12345
复制代码
Becomes:
  1. <body id="directorytest12345">0
复制代码
#08 - WordPress: Use the Slug

  1. <body id="directorytest12345">1
复制代码
Example:
  1. <body id="directorytest12345">2
复制代码
Becomes:
  1. <body id="directorytest12345">3
复制代码
#09 - WordPress: Conditionally Add Extra Classes

  This a body class to the ones generated by WordPress with conditional    if()logic. For example, here we're using the    is_shop()boolean logic provided by WooCommerce:  
  1. <body id="directorytest12345">4
复制代码
Becomes:
  1. <body id="directorytest12345">5
复制代码
#10 - WordPress: Add the Slug to the Body Class Defaults

  We can also alter the output of the    body_class()function by filtering it. We could do this in `functions.php` or our own functionality plugin.  
  Here we'll add the slug:
  1. <body id="directorytest12345">6
复制代码
Example:
  1. <body id="directorytest12345">7
复制代码
Becomes:
  1. <body id="directorytest12345">8
复制代码
#11 - WordPress: Add to Body Class using function.php Filter

  Add the following to functions.php:
  1. <body id="directorytest12345">9
复制代码
Then it would output like this, just calling    body_class()directly without doing anything else:  
  1. <?php
  2.   list($page, $class) = explode('?', $_SERVER['REQUEST_URI']);
  3.   $page = str_replace(array('/', '.php'), '', $page);
  4.   $class = str_replace(array('='), '', $class);
  5. ?>

  6. <body id="<?php echo $page ?>" class="<?php echo $class ?>">0
复制代码
#12 - WordPress: Raw Filtering of Function using Functions.php

  This is how you would add a single class of any name:
  1. <body id="directorytest12345">9
复制代码
Becomes:
  1. <?php
  2.   list($page, $class) = explode('?', $_SERVER['REQUEST_URI']);
  3.   $page = str_replace(array('/', '.php'), '', $page);
  4.   $class = str_replace(array('='), '', $class);
  5. ?>

  6. <body id="<?php echo $page ?>" class="<?php echo $class ?>">2
复制代码
#13 - WordPress: Raw Filtering of Function using Functions.php to Add Multiple Classes, Conditionally

  1. <?php
  2.   list($page, $class) = explode('?', $_SERVER['REQUEST_URI']);
  3.   $page = str_replace(array('/', '.php'), '', $page);
  4.   $class = str_replace(array('='), '', $class);
  5. ?>

  6. <body id="<?php echo $page ?>" class="<?php echo $class ?>">3
复制代码
Becomes this on a non-shop page:
  1. <?php
  2.   list($page, $class) = explode('?', $_SERVER['REQUEST_URI']);
  3.   $page = str_replace(array('/', '.php'), '', $page);
  4.   $class = str_replace(array('='), '', $class);
  5. ?>

  6. <body id="<?php echo $page ?>" class="<?php echo $class ?>">2
复制代码
Becomes this if the logic comes out    true, i.e., on a shop page:
友荐云推荐




上一篇:基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建 ...
下一篇:The LLVM+SDCC toolchain
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

兵团战神 发表于 2016-10-11 23:28:28
有空一起交流一下
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表