When theming Drupal and wanting to output a view there are occasions where using a view display (e.g. a page, or a block - perhaps placed within a custom region ;-) ), or using Views Attach will not suffice.
Instead, you can embed a view using the following PHP snippet:
(NOTE: you'll need to have the core PHP input filter enabled if embedding in a node body)
<?php
$view = views_get_view('VIEWNAME');
print $view->preview('default');
?>or, if you need to use an argument with the view:
<?php
$args = array(ARGUMENTS);
$view = views_get_view('VIEWNAME');
print $view->preview('default', $args);
?>UPDATE
It appears that I missed the very obvious solution to this problem (!) which, as pointed out by a couple of commenters, is to use the ‘Trim this field to a maximum length’ option within the settings for the node title field.
I’ll leave this post in place as I think it still serves as a useful guide on how to create template override files for views, but please be advised that for implementing truncation of a views node title it is easier to:
The problem:
Drupal’s default 'Footer message' area (to which you can add content via the admin section - Administer > Site configuration > Site information) will not parse PHP.
If, for example, you add a copyright message to your footer message area such as "© Copyright 2009" it'll be fine until the New Year begins and you have to go in and manually change it to "© Copyright 2010". So, instead, it would be great if you could handle the date dynamically using the following PHP code (see http://php.net/date for further PHP date details):
© Copyright <?php echo date('Y'); ?>However, try adding that to the default 'Footer message' area and it will simply output the entirety of the code as text, ignoring the PHP tags.

So, what to do?
Solution 1:
Create a block (admin/build/block/add) containing the code, set its input format to PHP, and then place that block in the footer region via the main blocks settings page (admin/build/block). Crazy simple.
The problem:
Whilst working on an aggregation site recently I needed to find a way to truncate the length of aggregated posts so that they only showed the first 200 or so characters of the post. However, I also wanted the split point in the post to occur at the end of a whole word, as opposed to part way through a word, and add some visual indication that there was more still to read.
The solution:
I'll first show the entire solution and then walk through how it works afterwards.
In your theme's node.tpl.php file the node content will most usually be output using code similar to:
<div class="content">
<?php print $content ?>
</div>Find this code and replace it with the following code (minus the opening and closing php tags which are just used here to improve the formatting):
<?php
<div class="content">
<?php
$mywords = explode(" ", $content);
$finalstring = "";
foreach($mywords as $word) {
if(strlen($finalstring) <= 197) {
$finalstring = $finalstring . " " . $word;
} else {
$finalstring = $finalstring . " <strong>[...]</strong>";
break;
}
}
echo $finalstring;
?>
</div>
?>I was working on a Drupal theme recently which required a number of different elements to be present on a number of different pages, but only for logged in users.
Using a little PHP I was able to add a class of 'in' to the body which would only appear in the html when a user was logged in.
To do this, just find the opening <body> tag in your page.tpl.php file, and add in the code below.
<body<?php global $user; ?><?php if ($user->uid): ?> class="in"<?php endif; ?>>Then, you'll be able to write a general rule for logged out users, such as:
body {
background: #fff;
}And a more specific rule for logged in users, such as:
body.in {
background: #000;
}I got the idea for today's post after I received an e-mail during the week from Colin who was wondering how I added a permalink, and then styled the links section so that everything (comments, read more, permalink etc.) lines up.
I thought this might be of general interest, so here's the answer.
It's basically just a two step process:
Step 1 - Add markup to the node.tpl.php file
To start with I just added some markup to the links div, and then positioned the whole links div at the end of my node.tpl.php file, right before the closing node div tag.
<?php if ($links): ?>
<div class="links">
<p class="permalink"><a href="<?php print $node_url ?>" title="Permalink to this my Drupal blog post">Permalink</a></p>
<?php print $links ?>
</div>
<?php endif; ?>This positioned the permalink within the links div before the other links.