Jump to content

[SOLVED] Count and summarize pages in multiple languages


asbjorn
 Share

Recommended Posts

Say I have a blog with 50 blog posts in Norwegian, and 10 of them are also translated into English. How do I use $pages->count() (or another method) to count them and show the total in a template file?

I have only gotten as far as getting the number 50 when viewing the Norwegian version, and the number 10 when viewing the English version of the page.

Link to comment
Share on other sites

  • 1 month later...

I found no other simpler solution, so I went for a loop through of languages, and summarized the count.

// Save current language
$savedLanguage = $user->language; 

// Loop through languages
// Find the other language
// Code is only for two languages as of now
foreach($languages as $language) {
  if  ($language->id == $savedLanguage->id) continue; 
  if(!$page->viewable($language)) continue;
  $user->language = $language;
  $languageCount = $pages->count("template=post");
}

// Revert to saved language
$user->language = $savedLanguage;

// Total count
// Add up current language count and the other
$postCount = $pages->count("template=post") + $languageCount;

// Output number
echo $postCount;

 

Link to comment
Share on other sites

  • asbjorn changed the title to [SOLVED] Count and summarize pages in multiple languages

Hi @Zeka. I tried this, and it worked when I was viewing the counted number on a page with my default language.

But when I switched to viewing it with my non-default language, I was not able to do the opposite. I could not get the "status1023" where the ID was of my default language to work. Any suggestions here?

Link to comment
Share on other sites

Hi @snobjorn

$currentLanguage = $this->wire()->user->language;
$languages = $this->wire()->languages;
$selector = new Selectors("template=some-template");

if($currentLanguage->id === $languages->getDefault()->id) {
	$selector->add(new Selectors("status=1"));
} elseif ($languages->findNonDefault()->has($currentLanguage)) {
	$selector->add(new Selectors("status{$currentLanguage->id}=1"));
}
bd($this->wire()->pages->find($selector)->count());

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...