phptemplatesdrupal-7content-type

drupal 7 custom content type and templates


... ok ... so ... i just needed to "clear cache" after all that. i thought because i don't have caching on (site is in development) i wouldn't need to clear it. wrong.

the solution was:

add the file node--my-content-type.tpl.php and go to Administration » Configuration » Development then click the clear cache button.

i hope this helps someone not spend hours on end solving this same problem!

Using Drupal 7.2, I have created a custom content type 'my_custom_type and I can't for the life of me figure out how to create a custom template for my custom type. My template file at the moment just prints "hello world", but no luck displaying it. I've tried these combos of things:

  1. putting node--my-custom-type.tpl.php in the my theme's templates directory. That didn't work. So I, after researching, added this to my THEME_preprocess_page() function in templates.php:

    if (isset($variables['node'])) {
    
        $variables['template_files'][] = 'node--'. 
                                         str_replace('_', 
                                                     '-',
                                                     $variables['node']->type);
    
    }
    
  2. putting that same code in THEME_preprocess_node() without the if, so:

        $variables['template_files'][] = 'node--'. 
                                         str_replace('_', 
                                                     '-',
                                                     $variables['node']->type);
    
  3. both of the above but with my tpl.php file in the base template directory: /modules/node/

Any help would be tremendously appreciated. I'm at a complete loss.

Also, I added print "what the what" in /modules/node/node.tpl.php and it printed.. maybe this is because the content-type isn't a node? but then how to create a default template for a content type?


Solution

  • It's not advisable to modify core files. See http://drupal.org/best-practices/do-not-hack-core. I'm not sure if this is what you're doing, but if you are...

    What you should do instead is create a subtheme. See guides at http://drupal.org/node/225125 and http://drupal.org/node/171194

    Usually you would put your custom theme files in /sites/all/themes/custom/subtheme_name/ node--my-custom-type.tpl.php.

    Remember to clear your cache at http://yoursite.com/admin/config/development/performance so that your new template files are recognized.