Community Documentation

CREATE CONTENT navigation block

Last updated December 17, 2009. Created by nimazuk on February 9, 2008.
Log in to edit this page.

You may use this code to make a block containing add content links.
This may look like the page at node/add

<?php
global $user;
$types = node_get_types();
   
// If no (valid) node type has been provided, display a node type overview.
   
foreach ($types as $type) {
      if (
function_exists($type->module .'_form') && node_access('create', $type->type)) {
       
$type_url_str = str_replace('_', '-', $type->type);
       
$title = t('Add a new @s.', array('@s' => $type->name));
       
$out = '<li>'. l(drupal_ucfirst($type->name), "node/add/$type_url_str", array('title' => $title)) .'<br/>';
       
$out .= filter_xss_admin($type->description) .'<br/>&nbsp;</li>';
       
$item[$type->type] = $out;
      }
    }

    if (isset(
$item)) {
     
uksort($item, 'strnatcasecmp');
     
$output = t('Choose the appropriate item from the list:') .'<ul>'. implode('', $item) .'</ul>';
    }
    else {
     
$output = t('No content types available.');
    }

  print
$output;
?>

Comments

Does this work for Drupal 6?

I've been looking for a snippet like this. I see it is tagged for drupal 5. Do we know if it works for drupal v6?

Edit: So far I've been able to get this to work in drupal v6 in case anyone else was interested.

Hi, I am using version

Hi, I am using version 6.14.
Not working though. I get 'No content types available.'
Any ideas how to adapt the code?

partially works

this partially works for me with Drupal 6.x, but with only 3 content types shown!

Only 2 content types appear

Similarly, in Drupal 6 I can only get 2 content types to appear. If I click on one of them, the remaining types appear. Help!?

About this page

Drupal version
Drupal 5.x, Drupal 6.x

Reference

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.
nobody click here