更改Wordpress Divi主题中的“阅读更多”文本

时间:2018-11-12 13:56:58

标签: php wordpress

我正在我的Wordpress网站中使用Divi主题创建页面。 我想将帖子中的“阅读更多”文本更改为“阅读文章->”。

我尝试使用优雅主题博客中的教程,但是没有用。

https://www.elegantthemes.com/blog/tips-tricks/how-to-customize-the-wordpress-read-more-link-text

如何使用php更改文本? (不是JavaScript)

posts screenshot

谢谢

4 个答案:

答案 0 :(得分:2)

您正在使用divi构建器。阅读更多的文本位于/divi-builder/includes/builder/module/Blog.php中的第1197行

要更改此内容,我们可以使用wordpress gettext过滤器(http://codex.wordpress.org/Plugin_API/Filter_Reference/gettext

将以下代码添加到主题functions.php文件中,它将解决您的问题。

function my_text_strings( $translated_text, $text, $domain ) {
    switch ( $translated_text ) {
        case 'read more' :
            $translated_text = __( 'read more ->', 'et_builder' );
            break;
    }
    return $translated_text;
}
add_filter( 'gettext', 'my_text_strings', 20, 3 );

我已经在我的wordpress网站中测试了该代码。它的工作。

答案 1 :(得分:1)

有多种方法可以做到这一点:

1。使用Jquery

在您的WordPress仪表板中,导航至Divi> Theme Options> Integration,然后将以下代码添加到“ Body”字段。注意:请确保“启用身体密码”开关处于打开状态。

<script type="text/javascript">
  (function($) {
    $(document).ready(function() {
      var newVal = 'Read Article';
      $('.more-link').html( newVal );
     });
  })(jQuery);
</script>

2。将此代码添加到主题functions.php文件中

// Replaces the excerpt "Read More" text by a link
function new_excerpt_more($more) {
   global $post;
   return '<a class="moretag" href="'. get_permalink($post->ID) . '"> Read the full 
   article...</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');

如果您使用的是子主题,则如果父主题具有自己的过滤器并设置了自己的“更多”链接,则上面的代码将无法进行修改。您将需要使用remove_filter()函数来删除父级的过滤器才能使用。问题是您的functions.php文件在父对象的functions.php之前加载,因此,在文件执行时,尚无要删除的过滤器,并且remove_filter()代码将失败而不会发出警告。

关键是将您的remove_filter()代码放入一个函数中,该函数从操作挂钩执行,该挂钩在加载父主题后触发。以下代码是使上述代码从父Divi主题的子主题开始工作所需的其他代码的示例。您需要检查实际父主题的代码中remove_filter()代码中的正确参数,它们必须与父主题使用的add_filter()参数完全匹配。

function child_theme_setup() {
   // override parent theme's 'more' text for excerpts
   remove_filter( 'excerpt_more', 'divi_auto_excerpt_more' ); 
   remove_filter( 'get_the_excerpt', 'divi_custom_excerpt_more' );
}
add_action( 'after_setup_theme', 'child_theme_setup' );

答案 2 :(得分:0)

也许您可以使用插件Loco Translate

答案 3 :(得分:0)

jQuery 方式与 ajax 分页:

jQuery(document).on('ready ajaxComplete', function() {
    // replace read more text
    var newVal = 'Weiterlesen';
    jQuery(".et_pb_post a.more-link").html(newVal);
});