Drupal:打印没有标记的字段

2021-12-30 00:00:00 php drupal

有没有办法在不获取所有标记的情况下打印字段内容?我是 Drupal 的新手,但我知道 field.tpl.php,但是,我只是想知道是否有一种更快的方法来获取节点中的内容——custom.tpl.php.它将与 Wordpress 的 <?php echo get_field('field_name'); 进行比较.?>

Is there a way to print field content without getting all the markup? I'm new to Drupal, but I'm aware of the field.tpl.php, however, I'm just wondering if there's a quicker way to get the content in a node--custom.tpl.php. It would compare to Wordpress's <?php echo get_field('field_name'); ?>

推荐答案

嗯,除了用field.tpl.php,我还能想到2个解决方案:

Well, apart from using field.tpl.php, I can think of 2 solutions:

第一:

使用 php 代码段去除 template.php 中的 html 标签.

Use a php snippet to strip html tags in your template.php.

在你的 template.php 中

in your template.php

function mytheme_strip_html_tags($n_field) {

   return preg_replace("/<.*?>/", "", $n_field);

}

然后调用函数mytheme_strip_html_tags($field_name)

但是,如果您使用多个主题,则需要将此代码段复制到每个主题中.

if you use several themes, however, you need to copy this snippet to each one of them.

您可以制作一个模块并将该片段放入其中.这样,它适用于每个主题.

You can make a module and place that snippet inside. This way it works with every theme.

第二个:

下载令牌模块.令牌是对您的字段的引用.Tokens 模块有一个输出模式,可以为你去除 html.[field_name-raw]

Download the tokens module. Tokens are references to your fields. Tokens module have a output mode that strips html for you. [field_name-raw]

您需要按照有关如何添加令牌的说明进行操作,但并不难.

You need to follow instructions in how to add tokens, but is not that difficult.

相关文章