0

I have a search result view of different content types, and i want to show the body field (trimmed) on some content types, but show no body on other content types. Since they all share the same body field, I would need to exclude the body field from displaying on the content types that I don't want to see them on.

Right now I am doing this with Views rewrite results and CSS. I add the content type field and exclude, then add the body field. Under rewrite results I rewrite the output of the body field with this:

<span class="search-body-[type]">[body]</span>

I then hide the content type bodies that I don't want to see with CSS:

span.search-body-MY-TYPE1,
span.search-body-MY-TYPE2 {
  display: none;
}

This works "visually", but the class and the trimmed body are still rendered in the HTML. Is there a better way to do this perhaps with a views_pre_render hook?

(I am completely inexperienced with PHP.)

0

This can be done really easily with views_conditional module. Once you enable the module, having the content type and body fields already added and excluded from display, do the following steps:

  1. add a views conditional field
  2. set the if condition to the [type] == Indexed Content: Content Type
  3. set is condition to contains
  4. Set the value to the content type name for the body that you want to display
  5. Then in Then output this... Use the [body] replacement pattern

Works like a charm!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.