I want to theme the search result view mode with a template.
I had it in my head that naming a template file node--article--search-result.tpl.php would do the trick, but I'm obviously wrong about that.
I realise I can do node--article.tpl.php and within this check $view_mode, but this is awkward with all the other view modes that I don't want to template.
Ideas?
Adding theme hook suggestions in a node preprocess function should do the trick:
function YOURMODULE_preprocess_node(&$vars) {
if ($vars['node']->type == 'article' && $vars['view_mode'] == 'search_result') {
$vars['theme_hook_suggestions'][] = 'node__article__search_result';
}
}
After clear the cache you should be able to use node--article--search-result.tpl.php for your template file name.
Hope it will help you. Happy coding.
Related
How can I assign an existing template file (template file of an another view) to Drupal view.
I already have a template views-view--search-issue.tpl.php for search_issue view. Is there any way to use the same template for another view archive_issue? Or is it necessary that I have to create a new template for that one?
Then you might want to implement some preprocess hook that lets you add some suggestions. Something like this (it's not tested and maybe you'll need a different hook but to get an idea):
function phptemplate_preprocess_views_view (&$vars) {
$view = $vars['view'];
if ($view->name == 'archive_issue') {
$vars['template_files'] = 'views-view--search-issue';
}
}
But like Aniruddhsinh said, the easiest way is to just copy paste the code you need in the appropriate template. Maybe you're feeling that you're violating the DRY manta (Don't Repeat Yourself), but in this case it's better than to break the pattern for views templates. Just go with Aniruddhsinh solution.
Source: Suggested template files for views
Go to your archive_issue view and select the template file name. Create a template file with the same name from archive_issue and copy the content from views-view--search-issue.tpl.php which is for your search_issue.Paste it into this archive's template file. Clear the cache because of template changes and you will get the same template as it is in search_issue.
I have page named page--news.tpl.php, which i created for my news page. But after i cleared my cache, page still not using, and drupal use the original page.tpl.php. Any ideas how to solve it?
An alternate way of doing it, is through preprocess hook with few lines of code.
Here's how it goes
function <module_name>_preprocess_page(&$variables) {
if (isset($variables['node'])) {
$variables['theme_hook_suggestions'][] = 'page__'.$variables['node']->type;
}
}
Suppose you have a node type as "news" then tpl should look like 'page--news.tpl.php' and above code will handle the rest.
I'm making a plugin where I want the comments in a single post page not to be printed at all. I need to query the database myself and print my own html with the results.
How can I make WordPress to not print the comments, without disabling them?
Thank you
EDIT:
as a suggestion, I am using:
apply_filters('comments_template', array($this,'comments_template'), 10, 1);
function comments_template($template){
$template = '';
return $template;
}
nothing happens, what am I doing wrong?
You could use the comments_template filter to make WordPress use your plugin's template file rather than the current theme's.
EDIT: based on your edited code: unfortunately you need to have an actual file, the path to which you return in $this->comments_template()...
class MyPlugin{
//add the filter somewhere...
function comments_template($template){
return dirname(__FILE__) . "/my_comments_template.php";
}
}
The file plugin_dir/my_comments_template.php must exist, otherwise WP falls back on the default theme's comments.php. See wp-includes/comment-template.php on lines 911-917.
In plugin_dir/my_comments_template.php you could call `MyPlugin::do_comments() or something like that. I don't know any other way around this. Let me know if you find a better way.
Cheers, Chris
Up to now, I've always hard coded what page template a certain page should use, either based on the URL, or what type of node it is.
What would be really useful is if there was a way to select which tpl file to use, right there in the node edit form. This would allow the user to flick between different page layouts at will.
Does anyone know a good way to approach this problem, or a flat out solution to it?
ThemeKey will let you load a theme based on a path or other criteria. You can use other methods like utilize preprocesser functions of template.php, and hook it in with hook_form_alter and come up with a way to switch files.
I ended up adding a new vocabulary for template files (the VID for this is 2 in my case) , and then rolled this into the page preprocessor in my template.php:
function phptemplate_preprocess_page(&$vars) {
if (count($vars[node]->taxonomy)>0)
foreach ($vars[node]->taxonomy as $term)
$template = $term->vid == 2 ? $term->name : NULL;
if ($template) $vars['template_files'][] = "template-".preg_replace("/[^a-zA-Z0-9s]/", "", strtolower($template));
}
Now if I have a node in a taxonomy term called: A Green Page! it will look for template-agreenpage.tpl.php as a template file.
I was wanting this functionality as well, so I made a module to do this very thing for node templates. You can find it here: http://drupal.org/project/template-picker
I've got a JS file that I want to add to Admin>Add Content>Certain Content type
After looking at template.php and checking out the function theme_preprocess_node
I tried to add the JS through drupal_add_js(...) but no go.
Now, I know that there's a similar question however my case is about adding
a JS file to a certain page and nothing else (better seperation of JS files).
Thanks.
(Drupal 6)
Check out drupal_add_js() in page template not working.
The gist of it is that calling drupal_add_js() (or drupal_add_css()) during preprocess functions is basically to late, as the markup for the js/css inclusion has already been rendered into a variable. To work around this, you need to overwrite the variable again by calling drupal_get_js() after your addition:
function yourThemeName_preprocess_page(&$variables) {
// Is this a node addition page for the specific content type?
// TODO: Adjust the content type to your needs
// NOTE: Will only work for the node add form - if you want your js to be
// included for edit forms as well, you need to enhance the check accordingly
if ('node' == arg(0) && 'add' == arg(1) && 'yourContentType' == arg(2)) {
// Add your js file as usual
drupal_add_js('path/to/your/file.js', 'theme');
// Ensure that the addition has any effect by repopulating the scripts variable
$variables['scripts'] = drupal_get_js();
}
}
NOTE: Use preprocess_page, not preprocess_node for this, as javascript inclusion should happen in the page template. Also, Kevins hint on the need to rebuild the theme registry still applies (+1).
Inside your module could you just do a:
function your_module_name_init() {
if(arg(0) == "some_name") {
drupal_add_js(drupal_get_path('module', 'your_module_name') . '/your_js_file_name.js');
}
}
Something like that?
After adding the drupal_add_js, did you clear the theme/site cache? That should work.