i would like to change the comments per page for only a single node. Let's say that the defaul number of comments per page for the content type "article" is 50 and i want change this to 10 only for the article with nid=171.
$node = menu_get_object('node',1); if($node && $node->nid = 171) {......}
Any hint? Thanks.
If you want to control the number of comments on individual pages, I would suggest installing Views module which allow for easy creation of any lists of things, including comments. With Views, you can create a block with comments for any node and use that instead of regular comments block.
Also, you can have several different blocks with different number of comments, and attach whatever block you want to whatever node. Using Context module would help too, it allows to assign blocks and other elements to pages with much more sophisticated controls than Drupal core block management.
Check out can this be helpful to you:
https://drupal.stackexchange.com/questions/7450/comment-per-page-settings
Since it's a form you have to alter it and change comments number from code.
i found a solution even if a bit rude. To disable the node's default comments i used a preprocess function in template.php file of my subtheme and disbled the $content['comments'] variable used in the comment-wrapper.tpl.php template:
function MYSUBTHEMENAME_preprocess_comment_wrapper(&$vars){
$node = menu_get_object();
if($node):
if($node->type === 'forum' && $node->nid == 171):
unset($vars['content']['comments']);
endif;
endif;
}
Note that i did it for comment wrapper variables/template and not for the node vfariables/template in order to unset the comments but to keep the comment form.
Then i created a view block of comments with a filter for the nid of the comment's node.
This works well for my purposes but if you find a more elegant way let me know. :)
I spent a time looking comment.module and found this in comment_form_node_type_form_alter
$form['comment']['comment_default_per_page'] = array(
'#type' => 'select',
'#title' => t('Comments per page'),
'#default_value' => variable_get('comment_default_per_page_' . $form['#node_type']->type, 50),
'#options' => _comment_per_page(),
);
So I put that in my template.php
function THEMENAME_preprocess_page(&$variables){
if (isset($variables['node']->type)) {
if ($variables['node']->type == 'forum') {
variable_set('comment_default_per_page_' . 'forum', 4);
}
}
}
It worked for me, I hope it helps.
I am using CakePHP 2.0 and I want to load a concrete CSS style in a Page. (with no controller but using the Layout).
I know it can be done with scripts using:
$this->Html->script('photos', array('inline' => false));
And then on the Layout:
<?php echo $scripts_for_layout; ?>
But i have no idea if it exists or not something similar to $scripts_for_layout; for CSS style.
Do you know how can i do it?
If you can upgrade to 2.1 then you can use this:
// in your view
$this->Html->script('carousel', array('block' => 'scriptBottom'));
// in your layout
echo $this->fetch('scriptBottom');
http://book.cakephp.org/2.0/en/views.html#using-blocks-for-script-and-css-files
update:
In earlier versions you could do it like you do with scripts:
$this->Html->css('some.css', null, array('inline' => false));
And it will be placed to $scripts_for_layout.
I'm working on a Drupal 7 website. I need custom layout for some pages. so I created page--customContentTypeName.tpl.php file and it addresses perfectly.
The problem is, I need to display some fields in page tpl. The code below works fine in node tpl, but page tpl :/
<?php print $content['field_images']['#items']['0']['filename']; ?>" />
How can I call custom fields into page tpl?
Appreciate helps!! thanks a lot!!
** SORTED **
with custom field editing... here is the tutorial video: http://lin-clark.com/blog/intro-drupal-7-theming-fields-and-nodes-templates#comment-54
For page.tpl.php
if you access the node directly you can use $node variable
$node['field_images']['und'][0]['filename']
else use $page variable.
$page['content']['system_main']['nodes'][1]['field_images']['#items'][0]['filename'];
but remember in a page variable you might have more than one node.
The structure changed in 7, the field is keyed by language first ("und" by default which means "undefined"), then you can follow this example:
// Array of Image values
$images = $node->field_images['und'];
//If you don't know the language, the value is stored in:
$node->language
// First image
$image = $images[0];
// If you need informations about the file itself (e.g. image resolution):
image_get_info( $image["filename"] );
// If you want to access the image, use the URI instead of the filename !
$public_filename = file_create_url( $image["uri"] );
// Either output the IMG tag directly,
$html = '<img src="'.$public_filename.'"/>';
// either use the built-in theme function.
$html = theme(
"image",
array(
"path" => $public_filename,
"title" => $image["title"]
)
);
Note the usage of the uri instead of the filename for embedding the image in a page because the File API in Drupal 7 is more abstracted (to make it easier to integrate with CDN services).
there are 2 useful modules in drupal for theme developers:
Devel and Theme_developer
Devel module provides a function called dsm() .
using dsm you can recognize that how elements are stored in different objects.
like nodes or ...
for example you can use this statement : dsm($node)
the structure of any nodes in the page will show up in the message box.
you can type the statements in your codes.
In Drupal 6, it was easy to insert a block into a template with the following code:
$block = module_invoke('views', 'block', 'view', 'block_name');
print $block['content'];
However, using the same instructions in Drupal 7 does not seem to work. I have looked around and cannot find the new method.
Does Drupal 7 have a routine that can allow for programmatically inserting a block into a template or node?
D7:
<?php
$block = module_invoke('module_name', 'block_view', 'block_delta');
print render($block['content']);
?>
'module_name' = The machine name of the module (i.e. the module's folder name). This is true for core modules too, so for instance 'search', 'user' and 'comment' would all work here.
'block_delta' = The machine name of the block. You can determine what this is by visiting the block administration page and editing the block. The URL for editing a webform block, for instance, would be something like:
Drupal 7: admin/structure/block/manage/webform/client-block-11/configure
In this example, 'webform' is the module's name, 'client-block-11' is the block's delta.
Custom blocks will have module name of 'block' and a number for a delta, which you can also find by editing the block.
More information: http://drupal.org/node/26502
This appears to be the solution for inserting blocks into templates for Drupal 7, but it seems a bit clunky and I have no idea about impact on performance:
$block = block_load('views', 'block_name');
$output = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));
print $output;
If anyone has a better procedure, please do add.
With wrburgess's answer you may get an error if your server is using a newer version of PHP.
Strict warning: Only variables should be passed by reference in include()...
This is what I did to not cause/get rid of the error.
<?php
$blockObject = block_load('views', 'block_name');
$block = _block_get_renderable_array(_block_render_blocks(array($blockObject)));
$output = drupal_render($block);
print $output;
?>
This work for me:
98 is the id of the block
$block =block_load('block',98);
$output = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));
print $output;
Just tested this in drupal 7 and it works:
$bloqueServicios = module_invoke('views', 'block_view', 'servicios-blo_home');
print render($bloqueServicios);
Good luck!
The module_invoke() function works. However, I found that rendering a block this way apparently won't use a custom template for that block. This might be OK depending upon your needs.
As commented before in other answers, this works as well and also makes use of custom templates:
$raw_block = block_load('your-module', 'delta');
$rendered_block = drupal_render(_block_get_renderable_array(_block_render_blocks(array($raw_block))));
print $rendered_block;
So, if you have a custom block--your-module--delta.tpl.php template file, it will be used to format the block.
Source: http://api.drupal.org/api/drupal/includes!module.inc/function/module_invoke/7
For some reason render() doesn't work for me, but this does:
<?php
$block = module_invoke('block', 'block_view', '1');
echo $block['content'];
?>
In my search to include a block in a template, i came across this post.
As an addition, if you want to include a custom block (that you added through the block interface) you have to use (instead of block_load(); in drupal 7)
$block = block_get_custom_block($bid);
$content = $block['body'];
Improving wrburgess' answer, you can do it in one line...
<?php print drupal_render(_block_get_renderable_array(_block_render_blocks(array(block_load('module_name', 'block_delta'))))); ?>
So for example, I use block number 6...
<?php print drupal_render(_block_get_renderable_array(_block_render_blocks(array(block_load('block', '6'))))); ?>
This worked for my Drupal 7 ,
URL: admin/structure/block/manage/addthis/addthis_block/configure
NOTE:delta and module name present in the url itself
$addblock = module_invoke('addthis','block_view','addthis_block');
print render($addblock['content']);
More information can be found on
http://technarco.com/drupal/insert-block-node-or-template-drupal-7
$block = module_invoke('menu_block', 'block_view', '6');
echo render ($block['content']);
This works for me for printing menu block.
There's module called insert_block for those which want to insert block "Drupal way" (not to program anything, just enable the module). Here's how to set it up.
NOTE: I know this question is about "programmatically inserting a block into a template or node" but Google sends people here even their are looking for non-programmer solution like me.
Have a look how Drupal does it in _block_render_blocks. The result of that function gets passed to drupal_render.
Recently I faced the same issue and I came across a nice solution which describes the solution in drupal as drupal's way.
You can print regions inside any template, but they aren't available out of the box in the node.tpl.php template. To make them available, you'll create a new variable for use in your node.tpl.php template that'll contain all the region content.
Creating new template variables is done by using a preprocess function. In your theme's template.php file, create a function that looks like this:
function mytheme_preprocess_node(&$variables) {
// Get a list of all the regions for this theme
foreach (system_region_list($GLOBALS['theme']) as $region_key => $region_name) {
// Get the content for each region and add it to the $region variable
if ($blocks = block_get_blocks_by_region($region_key)) {
$variables['region'][$region_key] = $blocks;
}
else {
$variables['region'][$region_key] = array();
}
}
}
Then, in your theme's node.tpl.php template, you can render any region by doing the following:
<?php print render($region['sidebar_first']); ?>
Where sidebar_first is the name of the region you want to render.
Read the complete article here: https://drupal.stackexchange.com/questions/20054/can-regions-be-printed-within-a-node-template
module_invoke Working fine for render block in the template file, but it's not working multilingual sites.
I'm looking for a plugin (or better yet, not a plugin) for wordpress that lets me generate standard content elements, or includes for posts and pages.
For example, my_content_1 could be:
buy it now for $23!!
Which could then be included in posts and pages using some kind of syntax (or whatever) like:
Welcome to my site, blah blah blah.. check out this product - %my_content_1%
Not looking for anything fancy, anything that does this sort of thing would be awesome.
The point of this being much like a regular php include I could have the same information updated in one place and applied over many pages/posts.
I found something that is pretty much what I'm looking for:
http://wordpress.org/extend/plugins/reusables/
However, other suggestions would be good as I'm not too confident in the quality of the code for that plugin.
Not sure about a plugin, but how about simply creating something yourself? If you created a PHP page and set up variables such as
$content->title = "This is a title"
$content->smallText = "Insert some short paragraph here"
And then just include it in your header? You could store it in your theme directory and then call it like so
<?php $themeFolder = get_bloginfo("template_url"); ?>
<?php include($themeFolder."/content.php") ?>
Would that be suitable?
How about creating a few files and link them in using shortcode?
ie: open your themes/functions.php file add this..
<?php
function wp_my_shortcodes($atts)
{
extract(shortcode_atts(array(
'type' => '', //author, rss, adverts
), $atts));
switch($type) {
case 'author' : $display = wp_display_author_info(); break;
case 'rssview' : $display = wp_display_rss_info(); break;
case 'adverts' : $display = wp_display_adverts(); break;
default : $display = wp_display_author_info(); break;
}
return $display ;
}
add_shortcode('mycontent', wp_my_shortcodes);
function wp_display_author_info()
{
include(TEMPLATEPATH.'/my_author_info.php');
}
function wp_display_rss_info()
{
include(TEMPLATEPATH.'/my_rss_info.php');
}
function wp_display_adverts()
{
include(TEMPLATEPATH.'/my_adverts.php');
}
?>
using shortcodes inside your posts you can then bring in which ever piece of content that you want.. in the example above I've created 3 pages in the template root folder called
my_author_info.php, my_rss_info.php, my_adverts.php all of which speak for themself..
my_author_info.php
this page could use the the_author_meta() to populate a div box with included author info,
my_rss_info.php
include your subscription box to let users subscribe to your blog
my_adverts.php
include 4x 125x125 adverts?
so in the post i could use
[mycontent type='author']
[mycontent type='rssview']
[mycontent type='adverts']
if no argument is added to the shortcode then the default view is shown, in this case..
[mycontent]
would return the authorview as default...
this would then include that file in the content...
just remember to create the included files :)
I found something that is pretty much what I'm looking for:
http://wordpress.org/extend/plugins/reusables/