php - 将阅读时间添加到 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.标签和阅读时间在国际/全球网站上有效。所有其他网站给我的信息是:您的网站存在严重错误。
如何更改功能以使其正常工作?
解决方案
以下是我如何使代码与@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>
推荐阅读
- java - 如何在 Spring Data jpa 上修复我的存储库创建
- ruby - 如何从 Sinatra 配置 Thin Web 服务器?
- vuejs2 - Vue cli 3 - 纽约伊斯坦布尔代码报告不包括 vue 文件
- python - python - 前6个元素:做循环然后取下6个元素,重复
- matrix - 连接可变结构的数组
- php - AWS:如何将 EC2 的 IAM 角色配置为 Web 服务器
- tensorflow - 张量流中的 Levenberg-Marquardt
- regex - 使用正则表达式提取包含搜索字符串的行
- jersey-2.0 - 当 Grizzley 服务器关闭时,泽西资源 @PreDestroy 没有被调用
- django - Django 自动在表单中添加外键