In learning, I've created a custom content type called story and a custom taxonomy called story_category. In story I have a term reference for story_category.
I can render a taxonomy term associated with a node however it automatically renders as a link to the URL alias instead of my custom taxonomy field.
How can I display a taxonomy term with no link?
My current result is the following.
These are the fields for the Story content up.
- title
- body
- field_impactful_story
- field_related_fund
- field_term_reference
- field_summary
These are the fields for the Story Category vocabulary.
- name
- description
- field_story_category_link
This is my page--front.tpl.php template.
<main>
<div class="container">
<div class="row">
<div class="col-sm-8">
<?php // ------ impactful stories ------ // ?>
<?php
$impactfulStoriesQuery = new EntityFieldQuery();
$impactfulStoriesQuery
->entityCondition( 'entity_type', 'node' )
->entityCondition( 'bundle', 'story' )
->propertyCondition( 'status', 1 )
->fieldCondition( 'field_impactful_story', 'value', 1, '=')
->propertyOrderBy( 'created', 'DESC' )
->range( 0, 2 );
$impactfulStoriesQueryResults = $impactfulStoriesQuery->execute();
if ( isset( $impactfulStoriesQueryResults[ 'node' ] ) ) {
$impactfulStoriesNodeIDs = array_keys( $impactfulStoriesQueryResults[ 'node' ] );
$impactfulStoriesNodes = node_load_multiple( $impactfulStoriesNodeIDs );
$impactfulStories = node_view_multiple( $impactfulStoriesNodes, 'full' );
print render( $impactfulStories );
}
?>
<?php // ------ end impactful stories ------ // ?>
</div>
<div class="col-sm-4">
<p>Some other content</p>
</div>
</div>
</div>
</main>
This is my node--story.tpl.php template.
<article class="story u-clearfix">
<img class="story-image u-floatLeft" src="http://lorempixel.com/image_output/people-q-c-200-200-4.jpg" alt="">
<div class="u-floatRight">
<h3 class="story-title">
<?php print render( $title ); ?>
</h3>
<p class="story-summary">
<?php if ( $impactfulStorySummary ): ?>
<?php print render( $impactfulStorySummary ); ?>
<?php endif; ?>
</p>
<div class="story-meta u-clearfix">
<?php if ( $impactfulStoryCategory ): ?>
<?php if ( $impactfulStoryCategoryLink ): ?>
<a class="u-floatLeft" href="<?php print render ( $impactfulStoryCategoryLink ); ?>">
<?php print render ( $impactfulStoryCategory ); ?>
</a>
<?php endif; ?>
<?php endif; ?>
<?php if ( $impactfulStoryRelatedFund ): ?>
<a class="u-floatRight" href="https://www.giveto.osu.edu/makeagift/?fund=<?php print render( $impactfulStoryRelatedFund ); ?>">
Give
</a>
<?php endif; ?>
</div>
</div>
</article>
This is the template.php file for my theme.
<?php
function osu_preprocess_node( &$variables ) {
// ------ preprocess impactful story nodes ------ //
if ( $variables[ 'node' ]->type == 'story' ) {
// get summary for the story content type
$summaryItem = field_get_items( 'node', $variables[ 'node' ], 'field_summary' );
$summaryView = field_view_value( 'node', $variables[ 'node' ], 'field_summary', $summaryItem[ 0 ] );
$variables[ 'impactfulStorySummary' ] = $summaryView;
// get the taxonomy term associated with a particular story
$categoryItem = field_get_items( 'node', $variables[ 'node' ], 'field_term_reference' );
$categoryView = field_view_value( 'node', $variables[ 'node' ], 'field_term_reference', $categoryItem[ 0 ] );
$variables[ 'impactfulStoryCategory' ] = $categoryView;
// get the term id so we can get term.
// once we have the term when can get a field of that term
$categoryID = $categoryItem[ 0 ][ 'tid' ];
$category = taxonomy_term_load( $categoryID );
$categoryLinkItem = field_get_items( 'taxonomy_term', $category, 'field_story_category_link' );
$categoryLinkView = field_view_value( 'taxonomy_term', $category, 'field_story_category_link', $categoryLinkItem[ 0 ] );
$variables[ 'impactfulStoryCategoryLink' ] = $categoryLinkView;
// get the related fund field associated with a story
$item = field_get_items( 'node', $variables[ 'node' ], 'field_related_fund' );
$view = field_view_value( 'node', $variables[ 'node' ], 'field_related_fund', $item[ 0 ] );
$variables[ 'impactfulStoryRelatedFund' ] = $view;
}
// ------ end preprocess impactful story nodes ------ //
}
?>
taxonomy_term_title( $term )
which does get you just a plain text title as long as you provide it a$term
. This seems pretty good to me. However$categoryItem = field_get_items( 'node', $variables[ 'node' ], 'field_term_reference' ); $categoryID = $categoryItem[ 0 ][ 'tid' ];
still feels like a dirty way to get a term from a node template.