Styling Individual Posts Using the_ID

How many times have you typed up a post and published it, only to wish that you could style that post just a little differently than the others. One example might be a special announcement post that maybe you wanted to highlight with a unique background color. Well we are going to take a look at one method of doing this in Wordpress.

Before we get started on the details I’d like to direct you to a short read in the Wordpress documentation on the

tag. I’ll wait…

Welcome back! Having read that maybe you noticed something interesting about one of the examples given for the tags usage. This tag can be used as a unique anchor identifier for each post. It can be used anywhere that you can place an ID or CLASS. Take the example they demonstrated:

firstexid.jpg

I’ve highlighted the important section above. The example headline has been given a unique attribute ID using the posts numerical ID within the name so that we can reference it individually within a stylesheet.

Now that we’ve seen that lets look at an example using a common theme that you can reference. On a default install of Wordpress you should have Kubrick installed. Log into your Wordpress Admin. Now go to Presentation: Theme Editor: Select Kubrick in the drop down menu: Main Index Template. If you scroll down while browsing the index you should see the following:

intropost_id.jpg

We see the familiar tag again and this time it is styling the post as a whole rather then an individual headline. How does this look when we publish? Kind of like this:

afterpublishing.jpg

Notice the unique ID of post-150. Each post that is published will have its own number (ie. 151, 152, 153, etc.) corresponding to that individual post. Now all we need to do is style it. To do this just open up your stylesheet and reference that attribute within the stylesheet. So in the case of Kubrick it could look something like this:

#post-150 { background: #113355; font-family: ‘Lucida Grande’, Verdana, Arial, Sans-Serif; }

You could even style any specific headlines within the above ID for that post like so:

#post-150 h2 {font-family: Georgia, Verdana, Arial; font-size: 1.2em; }

One last note in regards to using this when creating your templates. In XHTML, the ID attribute must not start with a digit. Since

the_ID

returns the post ID as number, you should include at least one alphabetical character before using it in an ID

attribute, hence the use of “post” within most templates.

Well thats it for now. We are going to come back to this later and take a look at a more flexible way of styling individual posts, because as you can imagine, your stylesheet could get quite bloated if you styled a large amount of posts using this method. But it does work great for those few posts that you need styled differently and I encourage you to give it a try.

What's Next?
Related Posts

Great tip! You can use to style individual comments in the same way.

Congratulations! You’ve our 1′000′000th commenter!

I’d like to know if that was me.

That meant to say…

You can use

comment_ID()

to style individual comments in the same way.

I’d like to see alternate background colors, author highlight, and single comment styling, all on one page :).

Craig - You were comment number 2698 and 2699.

There is an even more flexible way of doing this that I’ve been using for awhile. I’ll post it soon.

After that I’ll take a look at styling comments in various ways. Including author comments, ec.

I saw a complicated way to style every other post by adding a weird PHP call. Is there a way to do that using css only, or in a less complicated manner?

There is. You’ll see;)

@ brian: I’d love to see that in the next few days or so. ;)

Small Potatoes, I just wanted to say thank you for taking the time to fill this blog with a ton of incredibly useful info. Despite what your stat tracker may say, I swear I’m not stalking you - I just reference this site frequently for bits of information I haven’t found elsewhere! :D

@ Oriri: I’ll try to have it up by the end of the week. If not then I’m sure by next week. I’m going to try and make it as complete as possible.

@brian: That would rock, but seriously, no pressure! I’d love to read how you worked it! If it works as well as you hinted, I’m hoping it will be exactly what I need for a theme I’m working on for my blog.

(But I’ve got so much work left to do on the theme, before I hit the point in coding where I can use the background alternating, that you’ll probably have your post and blog done long before I finish this theme! )

Thank you as well for the response, and can’t wait to see what it is you’ve figured out! I can’t help but wonder if it will help me get what I’m hoping to do, done… ;)

That’s pretty awesome. But is there a way to do that with certain catagories? Because as soon as I read this, I was thinking, if it could be done, I would like to style my BLOGATHON posts to be nifty and stuffs.

@Jenny: There is a way to do it with certain categories actually. I might make that post first as it will be more simple to explain.

Yeah, I’d like to see this done with a certain category that I use only for important announcements and stuff. But even still, I had no idea you could do this with even individual posts. Good to know!

@Goob the category based styling should be posted soon.

[…] my previous post, the question came about if posts can be styled for a specific category. Well you’ll be happy […]

aha… that’s a great tip.

[…] via: wp-designer […]

[…] via: wp-designer […]

[…] via: wp-designer […]

25 неотстойных хаков для WordPress

Динамическое слайдшоу в шапке — позволяет крутить картинки в шапке вашего блока, придает динамику; демоверсия.
Пузырь комментариев — гр…

[…] Estilos diferentes para cada post. É bastante comum, tanto para posts que merecem destaque quanto para comentários. Faz aquela “zebra” nos comentários, sabe? Cor sim, cor não. […]

[…] Estilos diferentes para cada post. É bastante comum, tanto para posts que merecem destaque quanto para comentários. Faz aquela “zebra” nos comentários, sabe? Cor sim, cor não. […]

[…] Styling Individual Posts Using the_ID (Source: WP […]

[…] Styling Individual Posts Using the_ID (Source: WP […]

[…] Styling Individual Posts Using the_ID (Source: WP […]

[…] explains how to style individual posts using the post ID. This is an older post, but one I just discovered and I think it goes a long way towards showing […]

[…] Styling Individual Posts Using the_ID (Source: WP […]

[…] explains how to style individual posts using the post ID. This is an older post, but one I just discovered and I think it goes a long way towards showing […]

[…] Theme Hacks. 84. No More CSS Hacks. 85. Create a Dynamic Sticky. 86. Styling Individual Posts Using the_ID. 87. Show Category Images. 88. Separate WordPress Comments and Trackbacks. 89. Customize your […]

[…] ???? 83.WordPress Theme Hacks 84. No More CSS Hacks 85. Create a Dynamic Sticky 86. Styling Individual Posts Using the_ID 87. Show Category Images 88. Separate WordPress Comments and Trackbacks 89. Customize your […]

[…] WordPress ????83.WordPress Theme Hacks84. No More CSS Hacks85. Create a Dynamic Sticky86. Styling Individual Posts Using the_ID87. Show Category Images88. Separate WordPress Comments and Trackbacks89. Customize your WordPress […]

[…] 33. Styling Individual Posts Using the_ID […]

[…] 33. Styling Individual Posts Using the_ID […]

[…] Styling Individual Posts Using the_ID ( Sursa: WP […]

[…] Individualizando o post usando the_ID (Fonte: WP […]

[…] via: wp-designer […]

[…] Styling Individual Posts Using the_ID If you want to get creative, individual posts can be styled differently by using the post ID assigned by WP. […]

Andrei Gonzales:

I stumbled upon this gem of tip via Smashing Mag.

What I wanted to ask was: is there a way to make the loop call the specific stylesheet for that post? After 100 individually styled posts, a single stylesheet would be incredibly fat. Isn’t there a way to separate the main one (header, sidebar, footer, body), from the post-specific one?

-Andrei

Andrei Gonzales:

^^^ - Actually, scratch that. I think I just figured it out (was too lazy to use my brain 5 minutes ago :) )

[…] Styling Individual Posts Using the_ID […]

[…] Theme Hacks. 84. No More CSS Hacks. 85. Create a Dynamic Sticky. 86. Styling Individual Posts Using the_ID. 87. Show Category Images. 88. Separate WordPress Comments and Trackbacks. 89. Customize your […]

[…] Styling Individual Posts Using the_ID If you want to get creative, individual posts can be styled differently by using the post ID assigned by WP. […]

[…] Styling Individual Posts Using the_ID […]

[…] Styling Individual Posts […]

[…] 14?Styling Individual Posts ??????ID????????????????????????????? […]

[…] Styling Individual Posts […]

Reply
Comment Policy
  • Theme support questions should be posted at the support forums.
  • Name and Email are required. Email is never published.
  • You grant this site perpetual license to reproduce your words and name/website in attribution.
  • Inappropriate comments will be removed at my discretion.
Close
Powered by ShareThis