Created
May 19, 2010 18:18
-
-
Save roelven/406636 to your computer and use it in GitHub Desktop.
Retrieve a string in a specific language when using qTranslate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
How to print a string in both (or more) languages when using qTranslate on Wordpress | |
qTranslate provides Wordpress with multilanguage support and automates this in a pleasant way. | |
However you might want to print a specific string in a specific language, somewhere in your | |
theme or module. | |
Use this function to extract the content from the qTranslate quicktags, and retrieve the | |
title directly from the database using this query. | |
You can put the function in your functions.php. | |
Be careful with using SQL queries in your themefiles. This should never be a good idea! | |
This is just for demonstrating purposes since qTranslates' support fora isn't doing a better job. | |
qTranslate: http://www.qianqin.de/qtranslate/ | |
Wordpress wpdb class: http://codex.wordpress.org/Function_Reference/wpdb_Class | |
Your Neighbours: http://yourneighbours.de/ | |
*/ | |
function ynbs_translatethis($content) { | |
$regexp = '/<\!--:(\w+?)-->([^<]+?)<\!--:-->/i'; | |
if(preg_match_all($regexp, $content, $matches)) { | |
$output = array(); | |
$count = count($matches[0]); | |
for($i = 0; $i < $count; $i++) { | |
$output[$matches[1][$i]] = $matches[2][$i]; | |
} | |
return $output; | |
} else { | |
return 'no matches'; | |
} | |
} | |
global $wpdb; | |
$posttable = $wpdb->prefix.'posts'; | |
$raw_title = $wpdb->get_var($wpdb->prepare("SELECT post_title FROM $posttable WHERE id = $post_ID")); | |
$ynbs_post_title = ynbs_translatethis($raw_title); | |
print $ynbs_post_title['en']; | |
print $ynbs_post_title['de']; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome, thanks!