I'm trying to figure out how many articles( nodes? ) will be displayed in a listing on our website, to use it to add classes to the elements that are rendered, so one can easily have different styling depending on the number of frontend elements. Right now there are six elements, so preprocess_node(&$variables) is called 6 times in template.php in my theme, but: How can I obtain the number of nodes in the preprocess_page() - hook?

The nodes seems to be produced by a Promoted articles - type of listing.

  • Is your listing based on a view?
    – Linus
    Jan 26, 2016 at 12:52
  • No, it looks like it is a kind of Promoted articles - listing. It is used on the frontpage and several subpages on our website. The setup is quite complex, and to me at least quite confusing, I'm new at drupal but have experience in other systems.
    – jonasfh
    Jan 26, 2016 at 13:16
  • To get a specific answer, it might help to post your current code.
    – matt2000
    Jan 27, 2016 at 23:22

3 Answers 3


No need to modify template files. Instead create a view of Article content type and then follow the below steps.

  1. Into view Header part add field Global: Result summary
  2. Now into above field configuration, inside Display text area add @total token.

enter image description here

  • This is good to know, but it is not really what I'm asking, and for me this is not at all the easiest approach now. Thanks anyway.
    – jonasfh
    Jan 26, 2016 at 13:33

You can use the following code inside your THEME_preprocess_page():

function THEME_preprocess_page(&$variables) {
  $content_type = 'article';
  $total = 0;
  foreach($variables['page']['content']['system_main']['nodes'] as $nid => $node) {
    if($node['#bundle'] == $content_type) {
  //if you have installed the devel module you can use dpm() to see the result;


Remember clean the cache.

  • Using db_query in a theme function is usually not the right way to go, and often dangerous. And it seems the Asker wants to know the number of articles on the page, not necessarily the total number of articles in the system.
    – matt2000
    Jan 27, 2016 at 23:35
  • @matt2000 Oh, thanks, I misunderstood the question, I will edit the answer now, thanks for the comment. Jan 27, 2016 at 23:47
  • @matt2000 I edited the answer, now it works as expected ;-) Jan 27, 2016 at 23:57
  • This answer is sort of what I was looking for, but: only one node is in the nodes-array, but 4-6 are displayed. So I'm thinking these where found in some other array, but I'm still searching to find out where...
    – jonasfh
    Jan 28, 2016 at 10:43
  • @jonasfh in which page are you trying to see the info? I test in the <front> page only and it works, let me know the page to make some test Jan 28, 2016 at 14:07

I found the answer, and I'm posting this for reference:

$nid = $variables['node']->nid;
$arr = 
// counting number of elements with numeric keys: 
$num_elements = count(array_filter(array_keys($arr), 'is_int'));
// $num_elements contains the number of profiled articles. 
// this approach is similar to that of Adrians answer. 

// Nodes could also be found with
$num_elements = 

The main page had a single node containing a profiled_article - field. This field had a list of nodes that are displayed on the front page. Sorry for the incomplete description in the question, but I simply did not know where to look...


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.