I have a content type article and I need a specific template for showing articles according the number of nodes. If I have 2 nodes (content type article) I need to make a template, if I have 3 nodes (content type article) I need to make another template. How do you suggest to make that?

2 Answers 2


I don't know how to swap template files with panels I only know how to modify the layout based on condition, sorry. here's a small little snippet i quickly wrote to display a different template based on the number of nodes inside of the content type. This should really be done with a custom module but I don't have time.

  1. Make sure templates are in template folder under your theme and named correctly.
  2. Copy paste snippet below into your template.php file.

    function YOURTHEME_preprocess_node(&$vars) {
    if($vars['type'] == 'article'){
         // set content type
         $content_type = 'article';
         // Count number of articles in article
         $query = "SELECT COUNT(*) amount 
                   FROM {node} n ".
                  "WHERE n.type = :type";
         $result = db_query($query, array(':type' => $content_type))->fetch();
         // get number of articles
         $articleCount = $result->amount;
         //specify template based on number of articles
         // one - is equal to _ and two -- is equal to __ for template file
         // No need to add `.tpl.php`
         if($articleCount == 1){
             $vars['theme_hook_suggestion'] = 'page__template1';
         if($articleCount == 2){
             $vars['theme_hook_suggestion'] = 'page__template2';
         if($articleCount == 3){
             $vars['theme_hook_suggestion'] = 'page__template3';
         // ect.... you should make a dynamic loop here this is just basic logic.
  3. Modify code to your liking

  4. Clear Cache

count(node_load_multiple(array(), array('type' => 'The_Content_Type'))) will give you the number of nodes of type The_Content_Type.

The @DMAC solution is right but since you need to do this in Panels module so I give you this solution. In Panels select Selection rules and PHP Code

enter image description here

Now use this code

$nodes = node_load_multiple(array(), array('type' => 'The_Content_Type'));
 if(count($nodes) == 2)
     return TRUE;
     return False;  

You can clone this variant and replace 2 with 3 and modify the theme.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.