【问题标题】:Drupal aggregators and tailored outputDrupal 聚合器和定制输出
【发布时间】:2011-02-08 12:01:01
【问题描述】:

我的页面上有两个 Drupal 聚合器块,一个用于 Twitter 提要,一个用于博客提要。这两者提供信息的方式略有不同。

我希望博客提要显示标题,然后是帖子的前 80 个字符作为预告片。

我希望 Twitter 提要显示描述,而不是其他任何内容。这样做的原因是标题是状态的完整链接,而描述将其放在标准文本中并链接其中包含的任何 URL。

到目前为止,我有这个:

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;
}

这对我的博客提要非常有用,完美。但是,它显然会将我的 twitter 提要减少到 80 个字符。我尝试了 140 个字符的折衷方案,但由于它是 html 和所有字符,&lt;a href="..."&gt;&lt;/a&gt; 包含在 strlen() 中,所以它不是真的可行,特别是如果我在那里有一些链接,因为它会使我的博客描述预告片太长了。

所以,最后,我的问题是:我可以根据我想提供给哪个聚合器提要来定制输出吗?

我曾考虑通过获取页​​面上容器 div 的 ID 来做到这一点,但看起来这是行不通的。理想情况下,我最终会得到这样的代码:

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-description">' . $item->description . '</p>'; }

return $output;

抱歉这个问题太长了,感谢你的坚持!

【问题讨论】:

    标签: php drupal drupal-6


    【解决方案1】:

    我已经想通了。每个提要在 $item->fid 处都有一个 fid。我的代码现在是:

        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;
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-08
      • 2011-01-30
      • 2015-09-13
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      • 1970-01-01
      • 2018-09-26
      • 2021-08-13
      相关资源
      最近更新 更多