How can I extract and print specific field values from $page[‘content’] in page.tpl.php (vocabulary page)

I am trying to print some specific fields on a vocabulary term page in order to lay the page the way I want. <?php print render($page['content']); ?> prints the entire content but I want to extract specific values like title, field_photo etc. so that I can put them where and the way I want!

Here is an example how I want this to work:

I am creating a movie database website where each movie has casting associated with it. Each casting (actor or actress) is a vocabulary term. When I click on a term it takes me to a page (example URL: http://localhost/moviedb/tags/kate-winslet) where movies associated with that actor/actress are listed. The list is coming fine but I want to customize the look-n-feel of this page.

I overrode page.tpl with page--vocabulary--tags.tpl.php. Also I created a template.php file and written this function inside:

function introduction_preprocess_page(&$vars) { $vars['temp_var'] = $vars['page']['content']; }

which is equivalent of using $page['content'] directly inside .tpl file. I also used

<pre><?php print var_export(get_defined_vars(),TRUE) ?></pre> to get a the nested structure of nodes array. Based on that I tried to access a specific field like <?php print render $temp_var['title'] ?>, nothing is showing up.

I am definitely doing something wrong, but don’t know where to check and what to change! I have started learning Drupal just a few weeks back.

UPDATE

I also tried the following code but no luck yet.

function introduction_preprocess_page(&$variables) { if (arg(0) == 'node') { $variables['node_content'] =& $variables['page']['content']['system_main']['nodes'][arg(1)]; } }

INTRODUCTION is the name of my theme.

In .tpl file I tried to access title the following way:

if (isset($node_content) && $node_content['title']) { print render($node_content['title']); }

I have referred the following URLs:

https://www.drupal.org/node/2288941 and https://www.drupal.org/node/223430

Please help me understand what wrong I have done.

EDIT (27.05.2016)

I tried the solution but it did not work. By doing some trial and error I noticed that my node--film.tpl.php template is actually being used even on taxonomy page. In my application film is a content type I created and there is a field called “cast” which is eventually a vocabulary called “tags”. I am putting names of actors by using this vocabulary when adding a “film” type content.

My intention was to show a list of films where an actor is associated when a tag (actor) is clicked. The functionality works OK but I don’t understand why this page is also using node--film.tpl.php template file and not taxonomy-term.tpl.php. I copied taxonomy-term.tpl.php to my theme folder, took out the line which is rendering the content, cleared cached but nothing happened. But when I take out the same code snippet from my node--film.tpl.php file, the content disappears! Which confirms that node--film.tpl.php is the template being used!

I then renamed the file as taxonomy-term--vocabulary-tags.tpl.php just as a random test, but nothing changed either!

I am still unable to customize the look-n-feel and field display in my vocabulary page.

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

How can I extract and print specific field values from $page[‘content’] in page.tpl.php (vocabulary page)

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.