Another WordPress hack: Comments on Cross-Posts

! This post hasn't been updated in over a year. A lot can change in a year including my opinion and the amount of naughty words I use. There's a good chance that there's something in what's written below that someone will find objectionable. That's fine, if I tried to please everybody all of the time then I'd be a Lib Dem (remember them?) and I'm certainly not one of those. The point is, I'm not the kind of person to try and alter history in case I said something in the past that someone can use against me in the future but just remember that the person I was then isn't the person I am now nor the person I'll be in a year's time.

Although most of my blogging is done here, on Wonko’s World, I also author the West Midlands NO! Campaign blog and sometimes post on the Campaign for an English Parliament blog.

It’s common for bloggers who write on collaborative blogs to cross-post the same article on both their own blog and the collaborative blog they write on but what do you do about the comments? You might not necessarily want to have two seperate sets of comments on the same post. You can turn off the comments and put a link in the bottom of the post to the other post but they rarely get clicked.

Today I had a brainwave – add a metadata item with the url of the other post and change the comments link when the post is displayed. So, I tried it and it works and this is how to do it …

First up is the main index page of the blog. Open the index.php file and find comments_popup_link – this is the function that displays the comments link. The line should look like this:

<?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?>

Change it to this:

<?php
$xpostcommentsurl=get_post_meta($post->ID, "xpost", $single=true);
if(strlen($xpostcommentsurl)>0)
{
echo '<a href="'.$xpostcommentsurl.'" target="_blank">Comments</a> (off-site)';
}
else
{
comments_popup_link('No Comments', '1 Comment', '% Comments');
}
?>

Next we have to change the single post file – it should be called single.php. Open it and look for comments_template. The line should look like this:

<?php comments_template(); ?>

Change it to this:

<?php
$xpostcommentsurl=get_post_meta($post->ID, "xpost", $single=true);
if(strlen($xpostcommentsurl)>0)
{
echo 'This entry has been cross-posted from another site.';
echo 'Click <a href="'.$xpostcommentsurl.'" target="_blank">here</a> to open the comments page for this post.';
}
else
{
comments_template();
}
?>

One you’ve made these changes, everything is ready. When you write your post, scroll down the page to Custom Fields and add an entry named "xpost". Give it a value of the url of the cross-post you want the comments to appear on and save. Simple as that!

Technorati Technorati Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.