首页 > 解决方案 > 将阅读时间添加到 Wordpress

问题描述

我在 Wordpress 上有一个多站点,每个站点都针对公司设有办事处的一个国家/地区。所有网站都遵循相同的结构,只是内容从一个到另一个变化。
国际/全球网站为.com版本,国家使用目录:.com/dk/、. com /de/等。

我正在努力在帖子内容、标签和预计阅读时间之前显示。

functions.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);

  $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;
}

single.php补充说:

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

我有两个问题:
1. 我想在阅读时间之前添加“预计阅读时间: ”,它可以根据网站的语言而改变。
2.标签和阅读时间在国际/全球网站上有效。所有其他网站给我的信息是:您的网站存在严重错误。

如何更改功能以使其正常工作?

标签: phpwordpress

解决方案


以下是我如何使代码与@desinfor 的提示一起工作。我在single.php文件中添加:

<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>

推荐阅读