A scenario occurred recently with a theme that I have been developing where it needed to have each post section, each div, to be given the classname of the author who wrote the post.
This could be useful for targetting certain users, admin for example.
Generally with WordPress templates you have index.php for the homepage, and single.php for each individual post. Some themes, including mine, separate this even further and have another template post.php included from the single.php template.
Regardless of whether further templates are used, the layout tends to be like this:
<div class=”post”>
<div class=”post-title”><h2>Title</h2></div>
<div class=”post-content”>Content Here</div>
<div class=”post-bottom”>Posted by …</div>
</div>
With the introduction of WP-2.7 (Strayhorn), the new function post_class() was added, a very useful function. So this part…
<div class=”post”>
…now becomes…
<div <?php post_class(); ?>>
The post_class() function adds in all the necessary classes for each post, such as the category (if you post in the all category, the post_class() function adds the category-all class to the post div).
To modify this to add the name of the article’s author as a class to the div holding the post, you add in this bit of code and modify the post_class() as follows:
<?php $curauth = get_userdata(intval($post->post_author)); ?>
<div <?php post_class($curauth->user_login) ?>>
So that is very straightforward and from that point onwards the theme can customise posts for known/given users, such as admin, this might be useful on multi-author blogs.

















Trackbacks