asbjorn Posted June 30, 2021 Share Posted June 30, 2021 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 More sharing options...
asbjorn Posted August 9, 2021 Author Share Posted August 9, 2021 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 More sharing options...
Zeka Posted August 14, 2021 Share Posted August 14, 2021 Hi @snobjorn Probably you can use status in your count selector like $pages->find('template=post, status1023=1')->count() Where the 1023 is ID of your non-default langauge. Link to comment Share on other sites More sharing options...
asbjorn Posted August 16, 2021 Author Share Posted August 16, 2021 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 More sharing options...
Zeka Posted August 16, 2021 Share Posted August 16, 2021 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now