hello, I have two fields, one a taxonomy field and the other a numerical field. They are author and year. I want to keep the author year as a clickable taxonomy term whereas year is just plain text.

To me it seems that this should work, but it doesn't and the two are displayed on separate lines.

<div class="field field-type-content-taxonomy field-field-author">
  <h3 class="field-label">Author</h3>
  <div class="field-items">
    <?php foreach ((array)$node->field_author as $item) { ?>
      <div class="field-item" style"display:inline;"><?php print $item['view'].$node->field_author_year[0]['view']?></div>
    <?php } ?>
  </div>
</div>

The only way I can get them on the same line is to strip the tags from field_author, but then it's no longer a clickable taxonomy term which kind of ruins the whole point of it all! I've got this working with multiple images and it was my understanding that display could also be used for text. So what did I miss?

Comments

matthew_ellis24’s picture

For anyone with a similar problem such as mine, for example, on my site field_taxonomy outputs the following list:

term1 <br />
term2 <br />
term3 <br />

This is no good to me as I don't want the line breaks and the spaces at the end are annoying. There are two ways of removing the line breaks and spaces without stripping the tags:

1. PHP explode
use the php command explode. This will put all the parts of the variables into an array that you can call out into any format you want. The $taxon is just my choice of variable name, you can call it $pieces or $skhsgg if you want!

$taxon=explode(" <br />",$node->field_taxonomy[0]['view']);

This will explode the string "field_taxonomy" and separate it into little bits depending on where there is a space followed by
tag. In english this means that the string has been split up and is now accessed by using the variable name followed by the exploded part number, so with my example terms:
term1 = $taxon[0]
term2 = $taxon[1]
term3 = $taxon[2]
note the lack of line breaks!

If in doubt what the string is separated by then look at the page source and that will tell you. Then if for example you wanted to output these terms in one string you could use the following command:

print $taxon[0]." ".$taxon[1]." ".$taxon[2];

This will output:
term1 term2 term3
now you may notice I put the spaces back in, but I only took them out to demonstrate that you could. The explode command is very helpful if you have multiple terms in one variable and you want to rearrange the order because you can, for example, reverse them, or put them in any order you like:

print $taxon[2]." ".$taxon[0]." ".$taxon[1];

2. PHP str_replace
This is the php version of search and replace. The code below shows a working example. I've laboured over defining the variable at each step so it's clear how it all comes about. This code takes field_author and field_author_year and puts them together so you have "author, year" with the author tags intact.

$print=$item['view'].", ".$node->field_author_year[0]['view'];
$cleanedprint=str_replace(" <br />", '',$print);
print $cleanedprint;

The $print variable combines the author and year fields, but at this point it looks like this:

author <br />
year

I want them on the same line, so the $cleanedprint variable removes the line break and prints it out properly on one line.

Oh, the fullstops in the middle of the strings are there to combine different parts, it's like a + sign and it means you can print the string in one go, rather than needing many many print commands.

Hope this is of use to someone!

jrglasgow’s picture

Status: Active » Postponed (maintainer needs more info)

does this solve the problem, or is more help needed?

picxelplay’s picture

Just came across this post and noticed that your css is invalid.

style"display:inline;"

Should be:

style="display:inline;"
bassem’s picture

Version: 5.x-2.02 » 6.x-1.2
Status: Postponed (maintainer needs more info) » Active

I'm using Drupal 6, and i used the following code to display online labels but it doesn't seem to work

 <div class="field field-type-nodereference field-field-event-host-pr">
<div class="field-label" style="display:inline;">Hosted by:</div> 
<div class="field-items"><div id="host-value" class="field-item" style="display:inline;"><?php print $node->field_event_host_pr[0]['view'] ?></div>
 </div>
</div>

I'm still getting the label on one line and the value on the line below it, i tried styling it with the css style sheet using

.field-field-event-host-pr {
	display:inline;
}

this also does not work

picxelplay’s picture

@bassem Stick them in a span instead of a div

bassem’s picture

@picxelplay

thanks that worked