How to display custom CCK field (text or imagefield) in Drupal core search results page?
问题:
回答1:
You need to override search-result-tpl.php in your theme. Copy it from modules/search to your themes directory, clear the theme cache, and you're set. You'll see that there is an array available to the theme file called 'result', which contains a bunch of data, including a node object. So your file becomes something like:
<dt class="title">
<a href="<?php print $url; ?>"><?php print $title; ?></a>
</dt>
<dd>
<?php
// Here is the change
print $result['node']->field_name_of_cck_field['view'];
?>
<?php if ($snippet) : ?>
<p class="search-snippet"><?php print $snippet; ?></p>
<?php endif; ?>
<?php if ($info) : ?>
<p class="search-info"><?php print $info; ?></p>
<?php endif; ?>
</dd>
Good luck!
回答2:
1 Copy search-result.tpl.php file from modules/search to your theme folder
2 For CCK text field add:
<?php if ($result['node']->field_name[0]['value']): ?>
<h4><?php print($result['node']->field_name[0]['value']); ?></h4>
<?php endif; ?>
3 For imagefield with imagecache:
<?php if ($result['node']->field_logo[0]['filename']): ?>
<img src="/sites/default/files/imagecache/path_to_file/<?php print $result['node']->field_logo[0]['filename']; ?>" />
<?php endif; ?>
4 CSS styling next.
Thanx for cam8001 & googletorp!
回答3:
It depends how you do the search.
If you use views to build a search, you can decide yourself what to show.
If you're using some other search mechanics, you can use a combination of a proprocess hook, theming function, template you get the output you want. You should have the node object available, so displaying a CCK should be easy enough.
Edit:
For the Drupal core search module you need to overwrite the search-result.tpl.php in your theme, to alter how search results are printed. Here you can add or delete info. If you need more variables, you can create them for use in the template in a process hook. This is basic Drupal theming, check out the handbook.