使用php将文本链接替换为图像

时间:2012-05-20 17:19:20

标签: php drupal drupal-7

   l(t('Edit'), 'node/' . $node->nid . '/webform/components/' . $cid, array('query' => drupal_get_destination())),

我希望能够用图像替换“编辑”这个词,但是当我在其中放置图像时,它会显示为文本。我应该使用不同的功能吗?

我正在使用Drupal作为我的框架。

谢谢你从一个菜鸟。

3 个答案:

答案 0 :(得分:2)

执行此操作的最佳方法是合并l()theme_image()

print l(theme_image(array('path' => 'edit_button.png', 'attributes' => array('title' => t('Edit')))), 'node/' . $node->nid . '/webform/components/' . $cid, array('query' => drupal_get_destination(), 'html' => TRUE));

您还应该使用drupal_get_path()作为图片路径的前缀。 这样您就可以完全适用于Drupal Coding标准,让您的生活更轻松。

答案 1 :(得分:1)

该按钮应该有自己的类,例如action_button或id edit_button等ID。现在使用CSS隐藏文本并在其位置使用背景图像,如下所示:

.action_button, #edit_button {

  /* Hide Text */
  text-indent: -9999px;

  display: block;
  background: url('edit_button.png') no-repeat;

  /* Set width and height equal to the size of your image */
  width: 20px;
  height: 20px;
}

编辑1:

使用以下内容更改您的代码并使用上面的CSS:

l(t('Edit'), 'node/'.$node->nid.'/webform/components/'.$cid, array('attributes' => array('id' => array('edit_button')), 'query' => drupal_get_destination()));

编辑2 [最终]:

html作为TRUE传递给array parameter方法的l()可以让我们将first parameter用作img标记,而不仅仅是文字显示为链接:

l('<img src="edit_button.png" alt="Edit" />', 'node/'.$node->nid.'/webform/components/'.$cid, array('query' => drupal_get_destination(), 'html' => 'TRUE'));

答案 2 :(得分:0)

这是我实施的最佳方式:

list($nodeImage) = field_get_items('node', $node, 'uc_product_image');

$style_array = array('path' => $nodeImage['uri'], 'style_name' => 'MY_IMAGE_STYLE');

$render_node_image = theme('image_style', $style_array);   
$render_node_image_WITH_LINK = l($render_node_image, 'node/' . $node->nid, array('html' => TRUE));

print $render_node_image_WITH_LINK
相关问题