Pasko
Pasko

Reputation: 11

Polylang: string translation to "Read More" link not working

I can't seem to work my way around this: I have included a string translation into my functions.php file, Polylang registered it in the admin panel and I have added my translations for both Croatian and English.

When using the pll_e('saznaj-vise') function the output is fine in both languages (Saznaj Više and Read More), however when I include this string translation into my function modify_read_more_link() it does not display the permalink to the post/page, just static text above the content.

My code in functions.php looks like this:

function modify_read_more_link() {
  return  '<a href="' . get_permalink() . '">' . pll_e('saznaj-vise') . '</a>';
{

  add_filter( 'the_content_more_link', 'modify_read_more_link' );

  pll_register_string('read-more', 'saznaj-vise', 'Wordpress');

The code inside the loop (for displaying a page) looks like this:

<div>
  <?php global $more; $more = 0; ?>
  <p>
  <?php the_content(pll_get_post(5)); ?>
  </p>

What I would like is that my string translation becomes a permalink to the post/page on the desired cutoff, as usual in WordPress.

Upvotes: 0

Views: 6452

Answers (2)

Pasko
Pasko

Reputation: 11

This should be useful to anyone who wants to translate the Read More string, as the information online is quite hard to find. The working code in functions.php looks like this:

function modify_read_more_link() {
  return  '<a href="' . get_permalink() . '">' . pll__('string translation') .  '</a>';
}

add_filter( 'the_content_more_link', 'modify_read_more_link' );
pll_register_string('my-theme', 'string translation');

The problem was that the pll_e(string-translation) didn't work inside function modify_read_more_link() { instead we needed the polylang function which returns the translated string - the one with two underscores: pll__('string translation')

Simply replace 'string translation' with the name of your string and translate it inside the wordpress admin panel.

Polylang function reference sheet

Upvotes: 1

My setup includes the theme builder 'Divi' for a little bit of context.

So, in my case I assigned and id 'blogSPA' to the container of the blog posts. After that I found the class associated with the 'Read More', in this case was more-link.

What I did was a simple JS script that goes to the id I defined before 'blogSPA' and from there searches for all the 'more-link' classes and changes the innerText to 'LEER MÁS' (in my case spanish):

const blogSPA = document.getElementById('blogSPA');

const moreLinks = blogSPAquerySelectorAll('.more-link');

moreLinks.forEach(function(link) {link.innerText = 'LEER MÁS; });

In your case would be replace the 'LEER MÁS' with the text/lang that you want to translate. Please consider to reference the correct id according what you define.

After that you can include the script via plugin or directly in DIVI (in my case).

Upvotes: 0

Related Questions