Adding reading time to Wordpress

lela_rib :

I have a multisite on Wordpress where each site is for one country where the company has offices. All sites follow the same structure, only content changes from one to another.
The international/global website is the .com version, and the countries uses directories: .com/dk/, .com/de/ and so on.

I'm working to show before the post content, the tags and estimated reading time.

On functions.php I have:

function reading_time() {
  $content = get_post_field( 'post_content', $post->ID );
  $word_count = str_word_count( strip_tags( $content ) );
  $readingtime = ceil($word_count / 200);

  $lang = get_country_code();
  $timer['lang'] = $lang;

  if (in_array($lang, array('Global', 'UK', 'US', 'ME'))) {
    $timer = " minutes";
  } elseif (in_array($lang, array('CO', 'CL', 'MX', 'GT', 'CR', 'EC'))) {
    $timer = " minutos";
  } elseif (in_array($lang, array('DE', 'CH'))) {
    $timer = " Minuten";
  } elseif (in_array($lang, array('DK' ))) {
    $timer = " minutter";
  } elseif (in_array($lang, array('BR' ))) {
    $timer = " minutos";
  }

  $totalreadingtime = $readingtime . $timer;

  return $totalreadingtime;
}

And on single.php I added:

<div class="post-new-metas">
    <p class="post-tags"><?php the_tags(); ?></p>
    <p class="post-reading-time"><?php echo reading_time(); ?></p>
</div>

I'm having 2 troubles with it:
1. I want to add before the reading time "Estimated reading time:" that can change based on the language of the website.
2. The tags and reading time works on the international/global website. All other sites give me the message: You have a critical error on your website.

How can I change the function to allow this to work?

lela_rib :

Here's how I was able to make the code work with @desinfor's tip. I added on single.php file:

<div class="post-new-metas">
    <p class="post-tags"><?php the_tags( $before = '<b>Tags:</b> ' ); ?> |

    <?php
    function reading_time() {
      $content = get_post_field( 'post_content', $post->ID );
      $word_count = str_word_count( strip_tags( $content ) );
      $readingtime = ceil($word_count / 200);

      return $readingtime;
    }

    $readingtimeresult = reading_time();

    $html = sprintf(
        '<span>%s</span><span>%s</span>',
                __( '<b>Estimated reading time:</b> ', 'text_domain' ) . $readingtimeresult,
        __( ' minutes', 'Avada' ),
    );
    echo __( $html );
    ?>
    </p>
</div>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=345967&siteId=1