开发者

Drupal: Print field without markup

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 wonderi开发者_C百科ng 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'); ?>


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

first:

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

in your template.php

function mytheme_strip_html_tags($n_field) {

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

}

then call the function mytheme_strip_html_tags($field_name)

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

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

second:

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.


You have access to the $node variable inside a node.tpl.php, so:

<?php print $node->field_monkey_height; ?>

should work... note that many fields will hide their data inside arrays (for multiple value fields, etc) so you may need to do a bit of:

<?php drupal_set_message(print_r($node->field_monkey_height), 1); ?>

...to figure out the exact path to the data you need.


You simply can use PHP's strip_tags() like so:

<?php print strip_tags($node->field_name[LANGUAGE_NONE][0]['value']); ?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜