Support » Themes and Templates » <?php the_ID(); ?> in css?

<?php the_ID(); ?> in css?

  • I’m currently using the Minimal horizontal theme on a website I’m building. In index.php I added the code <div id="<?php the_ID(); ?>"> to line 11 so that in functions.php I could add <li><a href="#78">A-Z</li>. What this does is add an entry to the footer navigation that allows me to skip to a particular blog post, in this case post 78.

    What I’m wondering now is how do I reference <div id="<?php the_ID(); ?>"> in style.css> ? I tried adding the code <div id=”<?php the_ID(); ?>”> {background:#000000;} and it does nothing. Is there any way to fix this?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi

    1) use <div id="post-<?php the_ID(); ?>"> as ID’s have to start with alpha characters

    2) #post-78 {background:#000000;}

    if you have a number of posts that will all have the same background, it is easier to use a class

    <div id="post-<?php the_ID(); ?>" class="bkgrnd-1">
    .bkgrnd-1 {background:#000000;}
    Then you don’t require one CSS entry per post, only one per class.
    Assign “class=bkgrnd-1” to all posts that get black background.


    You can’t put PHP code in a CSS file

    You can generate a CSS file from a PHP script, making it dynamic AT PAGE LOAD. But then the stylesheet can’t be cached, meaning it has to be recreated on each page load.

    The reason using the_id() won’t work is that is only available within the WP loop. The CSS is loaded before the WordPress loop executes. So even if you could put it in the stylesheet, it would have no value assigned when the CSS file was generated.

    Hi there,

    I forgot to thank you for your advice. It worked great! Thanks again!


    Im having an issue with this theme and wanted to know if you could resolve it since your using the same theme “minimal horizontal”

    On the Index page there is the Nav on the bottom left (Home < > Navigate)

    How would i get this to show on all pages? It seems to only show on the index page. I have given up on trying to figure out how to do this.

    I though that there might be a code line inserted into Index.php that page.php didnt have and they seem the same.??

    Thank you

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘<?php the_ID(); ?> in css?’ is closed to new replies.
Skip to toolbar