Count the number of posts in a category in the current language only in Bogo

When I was creating a multilingual site using Bogo, I noticed that the number of posts displayed in the archive widget and category widget in the sidebar was the total number of posts for all languages. For example, if there are 3 Japanese posts and 2 English posts in a category, the number of posts will be displayed as 5, regardless of the page you are currently viewing. If you click on it, Bogo will work properly and only the articles in the current language will be listed, so if you are on the Japanese page, only the three Japanese articles will be displayed correctly. In the case of this blog, the number of posts in Japanese has increased quite a bit, so even if there are only three articles, it doesn’t seem like a big problem when it says “5 articles”. However, the English articles are not being translated at an adequate pace, so in that case, if the current display language is English, the number of articles should be zero for months or categories with no English articles, but the number of Japanese articles is displayed instead. When you click on it, of course an empty page appears because there are no English articles. I set up the site by disabling the number of articles display option in English mode, but I was frustrated, so I started looking into ways to fix the problem.

When I searched, I found two blogs that had similar problems and had taken measures to solve them. I tried to copy one of them, but for some reason it didn’t work, so I tried the other one and it worked. https://narwhale.net/post-366/ This is the site. It seems to be a system that hooks the wp_list_categories function and replaces it with the correct number of posts for each language obtained using WP_Query and displays it. It was easy to do, just by copying and pasting 10 lines of code into functions.php. The results are shown below. You can see that the post count is different for each language (Japanese and English).

Below shows the code to be added to functions.php.

function wp_list_categories_by_lang( $output, $args ) {
$lang = get_locale();
preg_match_all( '/<li class="cat-item cat-item-(\d+)"/', $output, $matches );
foreach ( $matches[1] as $id ) {
$q = new WP_Query( array( 'post_type' => 'post', 'cat' => $id, 'lang' => $lang ) );
$output = preg_replace( '/(<li class="cat-item cat-item-' . $id . '">.+)\(\d+\)/', '$1 (' . $q->found_posts . ')', $output );
}
return $output;
}
add_filter( 'wp_list_categories', 'wp_list_categories_by_lang', 10, 2 );

I thought that this was OK, but there was actually a small problem. When you display a list of articles in a certain category, the number of articles displayed for that category only becomes the total number of articles for all languages. In the example below, the list of 5 DIY category articles is displayed in English, but the number of articles displayed in the widget is 36, which is the total number of articles in English and Japanese. The next Audio category remains showing correct at 2 articles.

It made me feel sick, but I couldn’t think of a cause or a solution, and since there was no need to check the number of articles in the relevant category with the category list open, I decided to leave it as it was for the time being, thinking that there was no practical inconvenience to visitors.

I’m grateful to the pioneers who came up with easy countermeasures for the categories. I’d like to take this opportunity to thank them. As for the other issue in the posts number shown on archive list, I couldn’t find any information on the internet, so it took a little time. Please see my next post.

Comment

タイトルとURLをコピーしました