Drupal 6 - Custom page.tpl - css

I'm using Drupal 6. I have some css that I want to apply only for a specific content-type. The content types machine readble name is "snap".
I copied the page.tpl.php and created another file called page-snap.tpl.php.
I have restarted apache and mysql and refresh the cache but when I look for the content type snap to use as the DIV I see nothing.
What am I doing wrong?
Thanks,

You can use $node->type to include some additional css...
if($node->type == 'snap') {
//inlcude your css file
}

Take a look at Theming a page by content type
Instructions:
Rename page-snap.tpl.php to page-node-snap.tpl.php.
Create a file called template.php in your theme folder
In template.php, add this:
<?php
function bluemarine_preprocess_page(&$variables) {
if ($variables['node']->type == "snap") {
$variables['template_files'][] = 'page-node-snap';
drupal_add_css(PATH TO CSS FILE); // Enter path to custom css here
}
}
Save the file and clear the cache (admin/settings/performance)
More about drupal_add_css()

If you want different theme for different content type you can try themekey module. Using this module you can configure theme for different content types.

Related

Drupal basic page doesn’t seem to use page.tpl.php

Title says it really. Basic pages created in Drupal don’t seem to use the page.tpl.php file as a template.
If I edit the html.tpl.php file, those changes apply to every page, and it causes errors when I load a basic page.
I have also tried to copy the page.tpl.php file and name it page—basic-page.tpl.php to no avail.
Any idea what’s going on?
Also, how do I go about changing the page_top variable to include more content?
Lastly, the default page.tpl.php file has $page variables and things like $page_top and the like.
How would I call the title from the page only and the body text of a page only?
I’m using Drupal 7 and a custom sub theme.
The aforementioned files are in the template folder of the theme and I did clear caches when I added them.
Add $conf['theme_debug'] = TRUE; in settings.php and clear cache, reload page and check view source.
It will display the template file suggestions.
page.tpl.php file common for all pages. Just print anything to the tpl and run any node of basic page as well as other content type page and check if its working or not. If page.tpl.php not working for basic page only, then check your template.php file.
For print a page title just need to use following code:
<?php print $title; ?>
For print body text you need to use following:
<?php print render($page['content']); ?>
This may depend on the theme you are using. But I guess you are actually meaning page--page.tpl.php (double dashes). Which will be taken into account after you added the following snippet to your theme's template.php. Replace MYTHEME with your theme's machine name.
function MYTHEME_preprocess_page(&$variables) {
if (isset($variables['node']->type)) {
// If the content type's machine name is "my_machine_name" the file
// name will be "page--my-machine-name.tpl.php".
$variables['theme_hook_suggestions'][] = 'page__' . $variables['node']->type; // (double underscores)
}
}
See Template (theme hook) suggestions, where I also got above snippet from.
active theme debug for inspecting the template source and you get a different suggestions to user it (just avoid using node/nid).
commend drush to enable theme debug drush vset theme_debug 1

Load CSS and JS files only to a specific module Drupal

I’m developing a custom module, but need that only in the page where I use this module add some CSS and JavaScript files, I know how to do this from template.php using a condition to specific node, but I need do it more generic, that the condition can stay in .module file, or some similar solution.
I have a little code in my .module file:
function mymodule_init() {
$url = request_uri();
$path = drupal_get_path('module', 'module_name');
if ($url == 'xxxx') {
drupal_add_js($path . '/js/animations.js');
}
}
This work very well, but I’m limiting, because I need know the URL where the module will used.
Rather than checking for the URL, you should add your drupal_add_js when you initialize the code your module will be using on a given page.
For example, if you are adding a block, you could add it to hook_block_view. If you're adding a theme template, you could do it in hook_preprocess for that theme template.

custom html in drupal subtheme

I'm just starting out with drupal and need some custom html for an intricate menu system. My plan is to override the html-generating functions in template.php.
My theme name is "Drupal subtheme" and the navbar I would like to target has the machine name "menu-usm-navbar-small". What should I name the functions that overrides the default html-printouts?
I think I have tried every possible combination of these. Some examples of what I've tried:
function drupal_subtheme_menu_link($variables) {
return "foo";
}
function drupal_subtheme__menu_usm_navbar_small($variables) {
return "foo";
}
If you want to place custom HTML inside, why do you need to do it trough Drupal's functions?
Let's say, that you want to insert your code into page.tpl.php (most likely) - just open that file, edit it, add your code there.
Since you are overriding some theme - copy that file from original theme, and then edit it (don't forget to clear the cache).

Can my WordPress custom templates be in the plugin folder or only in the theme folder?

A WordPress theme I am developing has an integrated custom post type called "albums" which utilizes a few custom templates (archive-albums.php, content-albums.php, etc.). What I want to do is transfer this functionality, along with the template files, into a plugin for the sake of portability.
I transferred the CPT code from the functions.php with success, but when I try to move the template files from the theme folder to the plugin folder, things fall apart. I feel like it should be simple to somehow register the templates so WordPress knows to load them.
Can my WordPress custom templates be in plugin folder or only theme folder?
Things are falling apart because when you move those files, you're violating WP's native template hierarchy. You'll need to explicitly declare the location of those files. Using the archive as an example, you could add something like this to functions.php (to tell WP to look elsewhere):
add_filter('template_include', 'include_album_template', 1);
function include_album_template($template_path) {
if(get_post_type() == 'albums') {
if(!is_single()) {
$theme_file = 'path-to-your-plugin-directory';
$template_path = $theme_file;
}
}
return $template_path;
}
Obviously you'd use your own path, and I wrote this hastily so you might want to refactor.
I have the same issue. I'm already using add_filter ('template_include', ...) problem is that I need to specify a file to return, and in this case being it,index.php. This raises an issue with the theme not running entirely as if installed via themes folder, because what I need is to have WP selecting the appropriate file to render without any conditional logic from my part. So if it is a post it will select the single.php and so on. Another problem raised with this method is that in header.php the call get_header (); ignores the local file header.php and loads the default theme installed file instead.

how to override front page node tpl - drupal

How to override drupal front page node.tpl file ? I tried various
node--front.tpl.php
page--node--front.tpl.php
page--front--node.tpl.php
but its not working.
What will be the file name to override home page node ? (I am working in drupal 7)
You can add this function to theme template.php
function customethemename_preprocess_node ( &$vars ) {
if ($vars["is_front"]) {
$vars["theme_hook_suggestions"][] = "node__front";
}
}
Then you can page page--front.tpl.php
It will solve the problem
It should be page--front.tpl.php
Also, be sure that you have the precursors in the hierarchy for your theme (e.g. page.tpl.php)
I would recommend solving this by setting a specific node of content to be the front page.
http://www.inmotionhosting.com/support/edu/drupal-7/homepage/change-front-page
Then I would use a specific node ID template.
node--[insert id here].tpl.php i.e. node--1.tpl.php
You need to do two things before this will work:
Make sure you have a copy of the original node.tpl.php file in your theme folder (the overridden template file will not be picked up otherwise).
Clear Drupal's cache
No need to develop manually a front page, just creaate a frontpage by views or another and set it the as the front page in :
www.yoursite.com/?q=admin/config/site-information
Following steps resolved the issue for me to create custom front page in Drupal 7.
Create a file: page--front.tpl.php. Notice two hyphens instead of
one.
Clear the cache: Configuration >> Development >> Performance>>
Clear all caches
I think the best solution is using frontpage nid
function YOURTHEME_preprocess_node ( &$vars ) {
list(, $frontpage_nid) = explode('/', drupal_get_normal_path(variable_get('site_frontpage', 'node')));
if ($vars['node']->nid == $frontpage_nid) {
$vars['theme_hook_suggestions'][] = "node__frontpage";
}
}
Because
if ($vars["is_front"]) {
$vars["theme_hook_suggestions"][] = "node__front";
}
add the theme suggestion for all nodes in frontpage, not only for frontpage node

Resources