I have two Drupal aggregator blocks on my page, one for a Twitter feed, one for a blog feed. These both supply information in slightly different ways.
I would like the blog feed to display title, then the first 80 characters of the post as a teaser.
I would like the Twitter feed to display the description and nothing else. The reason for this is that the title is a whole link to the status whereas the description puts it in standard text and links any URLs included within it.
So far, I have this:
function mythemename_aggregator_block_item($item, $feed = 0) {
// Target p, div and images and preg_replace them with nothing
$tagstoreplace[0] = '/<p>/';
$tagstoreplace[1] = '/<div(.*?)>(.*?)<\/div>/im';
$tagstoreplace[2] = '/<img[^>]*>/';
// Generate output
$output = '<p class="feed-item-title"><a href="'. check_url($item->link) .'">'. check_plain($item->title) ."</a></p>\n";
$output .= '<p class="feed-item-description">' . substr(preg_replace($tagstoreplace, '', $item->description),0,80) . '...</p>';
return $output;
}
Which is great for my blog feed, perfect. However, it will obviously cut my twitter feed down to 80 chars. I've tried the compromise of 140 chars but with it being html and all, the <a href="..."></a>
is included in the strlen()
so it's not really workable, especially if I have a few links in there, as it would make my blog description teaser too long.
So, finally, my question is this: Can I tailor the output depending on which aggregator feed I want to supply it to?
I've thought about doing this by getting the ID of the container div on the page, but it looks like that's a no-go. Ideally, I'll end up with code like this:
function mythemename_aggregator_block_item($item, $feed = 0) {
// Target p, div and images and preg_replace them with nothing
$tagstoreplace[0] = '/<p>/';
$tagstoreplace[1] = '/<div(.*?)>(.*?)<\/div>/im';
$tagstoreplace[2] = '/<img[^>]*>/';
// Get feed type
$blog = //something;
$twitter = //something;
// Generate output
if($blog) {
$output = '<p class="feed-item-title"><a href="'. check_url($item->link) .'">'. check_plain($item->title) ."</a></p>\n";
$output .= '<p class="feed-item-description">' . substr(preg_replace($tagstoreplace, '', $item->description),0,80) . '...</p>';
}
if($twitter) { $output .= '<p class="feed-item-descriptio开发者_运维知识库n">' . $item->description . '</p>'; }
return $output;
Sorry about the length of this question and thanks for persevering!
I've figured this out. Each feed has a fid at $item->fid. My code now reads:
function mythemename_aggregator_block_item($item, $feed = 0) {
// Target p, div and images and preg_replace them with nothing
$tagstoreplace[0] = '/<p>/';
$tagstoreplace[1] = '/<div(.*?)>(.*?)<\/div>/im';
$tagstoreplace[2] = '/<img[^>]*>/';
// Display the external link to the item.
if($item->fid == '3') {
$output = '<p class="feed-item-title"><a href="'. check_url($item->link) .'">'. check_plain($item->title) ."</a></p>\n";
$output .= '<p class="feed-item-description">' . substr(preg_replace($tagstoreplace, '', $item->description),0,80) . '...</p>';
}
if($item->fid == '1') {
$output .= '<p class="feed-item-description">' . preg_replace($tagstoreplace, '', $item->description) . '</p>';
}
return $output;
}
精彩评论