The standard template for a node in Drupal is node.tpl.php
It's possible to call a different template for a content-type, like 'newsitem'. You will call it like this : node-newsitem.tpl.php.
I was wondering if there's a way to call a specific Node ID? node-34.tpl.php does not work.
Thanks
For Drupal 7 use this template name (34 - is node ID):
node--34.tpl.php
And don't forget to clear your cache! More information on drupal.org
In your theme's template.php put the following at the top of theme_preprocess_node():
$vars['template_files'][] = 'node-'. $vars['node']->nid;
So if your theme is called "myTheme", you might have the following:
function myTheme_preprocess_node(&$vars){
$vars['template_files'][] = 'node-'. $vars['node']->nid;
}
I have this working as we speak. In Drupal 6, my front page is node 5. It uses
page-node-5.tpl.php
If it's not loading, consider clearing cache's or rebuilding your theme registry.
That naming convention will work, just not by default. Assuming this is Drupal 6, try adding the following code to your theme's template.php:
/**
* Override or insert variables into the node templates.
*
* #param $vars
* An array of variables to pass to the theme template.
* #param $hook
* The name of the template being rendered ("node" in this case.)
*/
function yourthemename_preprocess_node(&$vars, $hook) {
$node = $vars['node'];
$vars['template_file'] = 'node-'. $node->nid;
}
Make sure you don't try to redeclare yourthemename_preprocess_node()--that is, if it already exists in your theme's template.php, just add the $node and $vars['template_file'] lines to it.
Related
I searched everywhere and could not find.
Now you are my only hope, please help.
Custom page template for global use , don't showing up in wordpress template dropdown when i create custom floder for the template parts.
My-theme/global_template.php WORK
My-theme/template-parts/global_template.php WORK
My-theme/template-parts/archive/global_template.php DON'T WORK
How can I make folders that inside template-parts that contain templates to appear in page attributes->template?
Here the code inside global_template.php
As you see there is no test template..
Your problem is the depth the function thats get the templates have 1 depth. so you must put the files in the theme root or first sub-folder
The core functions get_post_templates() call get_files() with depth 1 and it pass it on to the function scandir() that scan the directories.
You can add more templates manually like this replace the {$post_type} in the add_filter with the post type that you want to add the templates to. for example page.
/**
* #param array $post_templates Array of page templates.
* Keys are filenames, values are translated names.
*/
function extend_post_type_templates($post_templates, $this, $post, $post_type) {
$post_templates['template-parts/archive/global_template.php'] = 'Name for this';
return $post_templates;
}
add_filter( 'theme_{$post_type}_templates', 'extend_post_type_templates', 10, 4 );
Trying to fetch output in A.tpl but not getting any output. I think i'm doing something wrong to call php function in tpl file.
A.tpl
{myModifier}
B.php
class Geolocation{
public function sm_loc($params, Smarty_Internal_Template $template)
{
return "100.70";
}
}
$smarty1 = new Smarty();
$smarty1->registerPlugin('modifier', 'myModifier', array('Geolocation', 'sm_loc'));
I 've already used this code. And this doesn't seem to work. It also breaks my existing working code in A.tpl post this use.
My Need here is to get output from the php function in A.tpl from an external php file.
Thanks in Advance. Sorry for being noob.
To add this modifier to Smarty and use it in your template, you're best to use a WHMCS hook.
If you create a new PHP file under the '~/includes/hooks/' directory (you can name it anything - in this case, let's use 'myhook.php'), WHMCS will automatically inject this hook on each request.
For this, you're going to want to use the ClientAreaPage hook. Inside your hook, you can then access the global $smarty variable.
Example:
function MySmartyModifierHook(array $vars) {
global $smarty;
// I recommend putting your Geolocation class in a separate PHP file,
// and using 'include()' here instead.
class Geolocation{
public function sm_loc($params, Smarty_Internal_Template $template) {
return "100.70";
}
}
// Register the Smarty plugin
$smarty->registerPlugin('modifier', 'myModifier', array('Geolocation', 'sm_loc'));
}
// Assign the hook
add_hook('ClientAreaPage', 1, 'MySmartyModifierHook');
That should do the trick. If you want to explore with other hooks, you can take a look at the Hook Index in the WHMCS documentation.
The function names in each of your hook files must be unique.
As a side note, if you're only wanting to run this hook on specific pages, you can check the templatefile key in the passed $vars array. For example, say you only wanted this hook to run on the 'View Cart' page on the order form:
function MySmartyModifierHook(array $vars) {
global $smarty;
// If the current template is not 'viewcart', then return
if ($vars['templatefile'] != 'viewcart')
return;
// ... your code here ...
}
Also, note that with hooks like the 'ClientAreaPage' hook, returning an array of keys and values will automatically add them as Smarty variables. So if your hook function ended with return ['currentTime' => time()];, you could then use {$currentTime} in your Smarty template to output its value.
I am trying to unhook and modify an action from within my child themes functions.php file.
The WordPress plugin Sensei, adds this action in line 86 of this document.
https://github.com/Automattic/sensei/blob/master/includes/class-sensei-modules.php#L86
The action references a function further down the page that is responsible for outputting a dynamic header element.
/**
* Show the title modules on the single course template.
*
* Function is hooked into sensei_single_course_modules_before.
*
* #since 1.8.0
* #return void
*/
public function course_modules_title( ) {
if( sensei_module_has_lessons() ){
echo '<header><h2>' . __('Modules', 'woothemes-sensei') . '</h2></header>';
}
}
My goal here is to change the html currently output as 'Modules' to something else.
I have tried to the following in my child themes functions.php file but neither seem to be working.
remove_action( 'sensei_single_course_modules_before', array( 'Sensei_Core_Modules', 'course_modules_title' ), 20);
remove_action( 'sensei_single_course_modules_before', array( 'Sensei()->Sensei_Core_Modules', 'course_modules_title' ), 20);
The issue is, I do not know how to determine which initial parameter, to add to the array to call the correct class. Because I am accessing it externally I cannot use $this like it is being used in the core file.
In order to remove the action you have to find the instance of the class. This is an assumption since I don't have access to the source code of Sensei but big chance there is one since most WordPress plug-ins use this method.
When you find the instance name you can load it using global $senseiInstance - replace this with the actual name of the variable.
Then you can remove the action using for example this code:
remove_action( 'sensei_single_course_modules_before', array( $senseiInstance, 'course_modules_title' ), 20);
More information can be found in for example this article: https://www.sitepoint.com/digging-deeper-wordpress-hooks-filters.
Hope this helps you out!
I have a php function in a drupal module. This function outputs some random text. I want to attach this module to a Drupal article so that each time someone creates an article, random text will appear in it. How can I do this?
The solution here is to use a Drupal hook function to modify the content of the node.
Assuming your module is called "my_module":, you'd add another function to your my_module.module file as follows:
function my_module_node_view(&$node, $view_mode, $langcode) {
// We want to make sure this only applies to nodes of the content type "article"
if ($node->type == "article") {
// Append the output of your function to the body; this could easily be added to any other field as well
$node->body['und'][0]['value'] = $node->body['und'][0]['value'] . my_module_random_text_function();
}
}
Note: the $node object is automatically passed to this hook function by reference, so you don't need to worry about returning anything from the function.
If you wanted to apply this at the theme layer, you could use the theme_preprocess_node hook in your theme's template.php file, but your original question suggested that you'd already gone down the plugin route.
in template.php many times they used classes_array..am not getting the meaning and why they using,..what is the purpose of classes_array and when we have to use that in drupal7 .tpl.php
example code:
if(in_array('administrator',array_values($variables['user']->roles)))
{
$variables['classes_array'][]="debug";
}
$variables['classes_array'] is used in preprocess functions. It adds classes to be used in rendering the element to be processed. In your example, a class named "debug" will be added to the html container of the rendered element: if the actual code is
function <YOUR THEME>_preprocess_html(&$variables) {
if (in_array('administrator',array_values($variables['user']->roles))) {
$variables['classes_array'][]="debug";
}
}
your theme will output a body tag like
<body class='debug [...OTHER CLASSES...]'>
for users with administrator role.
You can also add classes to nodes, or other kind of elements for which a preprocess hook is available. E.g. you could write a node preprocess function:
function <YOUR THEME>_preprocess_node($variables) {
$classes_array[] = 'my-class';
}
if you wanted to add 'my-class' to every node of your site.
In general, you will not find $classes_array among the defined variables in tpl.php files. Your theme will, most of the times, implode them in a $classes variable. It must be noted, however, that a kind of confusion arised over time, so different themes may use $classes_array, $attribute_array, $classes, $attributes['class'] and so on for the same purpose, so you should check your theme's documentation to find out what suits your case.