No doubt you've encountered a blog at one time and noticed the custom styling applied to comments left by the author. It certainly adds a nice touch - and I wanted to be able to do the same here on Blogger. My first step towards finding a solution was to examine the comment markup generated by blogger. Unfortunately, however, it seems that Blogger doesn't indicate author comments in any way that can be accessed through a CSS rule. Well, I'm not that easily deterred, and eventually I came up with the following approach.
Before I explain how to implement this solution, let me briefly explain how it works. If you have ever looked inside your template's html, you have likely noticed some custom tags being used. In our case, we're concerned with the data tag. If this already sounds unfamiliar to you, I would advice you look at this page on using the data tag.
The particular piece of data that we want to access is the data.userUrl member. This member is only made available within the Profile widget, which unfortunately means that if you have disabled the profile on your blog, you're out of luck. This member is also not available if you have a team blog (sorry). For the rest of you bloggers (which is still the vast majority of you) this solution will work just great.
So now, with the preliminaries out of the way, we're going to tell the script to loop through all of the comments on the page. If the member link on the current comment matches the member link in the profile, then it must be an author comment. The script will then apply an additional class to the comment, allowing you to style it separately. All clear?
Step 1: Import the jQuery Library
In order for this script to work, you will have to import the jQuery library. Since jQuery let's you directly link to the source, this step is quite trivial. From your blog's dashboard, you will want to go to the layout tab, and then click "Edit HTML". Search for the <head> tag, and insert the following line directly below it:
<script src='http://code.jquery.com/jquery-latest.min.js' type='text/javascript'/>
Click "Save Template". Leave this page open because we'll be needing it in the following steps.
Step 2: Insert the Script
Now click the check box that says "Expand Widget Templates". Since the data.userUrl member is only available within the Profile widget, that is where we need to insert the script.
If are the only author of your blog, do this:
Search for the following tag:
<b:widget id='Profile1' locked='false' title='xxx' type='Profile'>.
Inside of this tag you will find the following line:
<b:else/> <!-- normal blog profile -->
Directly below this line, insert the following script:
<script type='text/javascript'>
$().ready(function() {
$('dl#comments-block dt a').each(function(i) {
if($(this).attr('href') == '<data:userUrl/>') {
$(this).parent('dt').addClass('author-comment').next('dd.comment-body').addClass('author-comment').next('dd.comment-footer').addClass('author-comment');
}
});
});
</script>
If you have co-authors on your blog, do this:
Search for the following tag:
<b:widget id='Profile1' locked='false' title='xxx' type='Profile'>.
Inside this tag, you will find the following tag:
<b:if cond='data:team == "true"'>.
Immediately beneath this tag, insert the following code:
<script type="text/javascript"> var author_urls = new Array(); </script>
Next, find the following line: <b:loop values='data:authors' var='i'>
Immediately below this line, insert the following code:
<script type="text/javascript"> author_urls[author_urls.length] = '<data:i.userUrl/>'; </script>
Next, insert the following code right below the closing ul tag.
<script type='text/javascript'>
$().ready(function() {
var urlsReg = new RegExp(author_urls.join("|"));
$('dl#comments-block dt a').each(function(i) {
if(urlsReg.test($(this).attr('href'))) {
$(this).parent('dt').addClass('author-comment').next('dd.comment-body').addClass('author-comment').next('dd.comment-footer').addClass('author-comment');
}
});
});
</script>
That takes care of all the JavaScript parts. Now all that's left to do is to style it.
Step 3: Update your CSS
If you look at the markup that Blogger generates, you will notice that each comment is made up of a dt tag followed by two dd tags. At the time of writing this, I am using the following additional CSS:
dl#comments-block dt.author-comment {
background-image: none;
background-color: #EDE5BE;
margin-bottom: 0px;
padding: 6px 0 6px 10px;
border: 1px solid #ccc;
border-bottom: 1px solid #FFF7CF;
}
dl#comments-block dd.comment-body.author-comment {
color: #593622;
background-color: #EDE5BE;
margin-top: 0px;
margin-bottom: 0px;
padding: 10px;
border: 1px solid#ccc;
border-top: 1px solid #CBC4AC;
}
dl#comments-block dd.comment-footer.author-comment {
font-size: .8em;
background-color: #CBC39C;
padding: 3px;
margin-top: 0px;
float: right;
border: 1px solid #ccc;
border-top: none;
}
To add these styles, simply paste them right above the </style> tag.
You're Done!
That's it! Just hit "Save Template" and view your blog. To see the script in action, view my comment below this post.
