Get Site Index from two languages

Hi there,

I want to loop through all pages, means in both languages. This is what I get so far, but this results only in one language … is there an easy way to get both?

foreach( $site->index() as $counts ) {
  ...
}

What is your use case? You would have to loop through. the languages as well and then get all the translations you need to output your content in each language.

Have a look at the sitemap plugin: https://github.com/omz13/kirby3-xmlsitemap

That was also my idea, but I’m not sure … seems there is a problem with my code, I get strange output. Every page has a field called “seitenaufrufe” which get’s updated on every pageview. Now I want to show all “seitenaufrufe” from all pages. Here’s the code:

$counter = 0;

foreach ( kirby()->languages() as $lang ) {
  
  foreach ( $site->index() AS $count ) {
    
    if ( $count->seitenaufrufe()->int() ) {
      
      $counter = $counter + $count->seitenaufrufe()->int();
      
    }
    
  }
  
}

echo $counter;

Your if statement doesn’t make sense, check if the field contains content something, secondly you have to get the content per language:

<?php
$counter = 0;

foreach ( kirby()->languages() as $lang ) {
  
  foreach ( $site->index() as $page ) {
    
    if ( $page->content($lang)->seitenaufrufe()->isNotEmpty() ) {
      
      $counter = $counter + $page->content($lang)->seitenaufrufe()->int();
      
    }
   
  }
  
}

echo $counter;

Don’t think you need the if statement if there’s nothing in the field, then it’s not added anyway.

You’re absolute right, that doesn’t make really sense. I also have modified, so it makes more sense for me in my brain :smiley:

$counter = 0;
  
  foreach ( $site->index() as $pageCount ) {
    
    foreach ( kirby()->languages() as $lang ) {
    
      if ( $pageCount->content($lang)->seitenaufrufe()->isNotEmpty() ) {
      
        $counter = $counter + $pageCount->content($lang)->seitenaufrufe()->int();
      
      }
   
    }
  
  }
  
  echo $counter; 

Thanks so much :+1: