Post navigation

code snippets

WordPress function to show correct QTranslate post title

Was using database queries to retrieve Post Titles in some of my template files, but unfortunately it was returning the title in ALL languages including the QTranslate tags…

<!--:en-->Aberdeen<!--:--><!--:de-->Aberdeen<!--:--><!--:zh-->Aberdeen<!--:-->

So I made a function with regex to find the post title of the correct language and then remove the excess tags, placed in my functions.php file:

<?php
// Function to get correct QTranslate post title
function getTransText($text) {
  // regex using correct language
  $lang=qtrans_getLanguage();
  preg_match('/<!--:'.$lang.'-->(.*?)<!--:-->/', $text, $matches);

  // Returns correct version of Post Title
  return strip_tags($matches[0]);
}
?>

Then I could just get the correct language title using

<?php
echo getTransText($text);
?>

Let me know if you have a better/smarter/smaller way of doing the same thing!

About David Goodman

2 thoughts on “WordPress function to show correct QTranslate post title

Leave a Reply

Your email address will not be published. Required fields are marked *

Please enable javascript to post a comment !