Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Karma theme and qTranslate help WordPress

  • REFUNDED

Hi,

Two question that'll probably have the same answer.
The website I'm working on is multilingual. Everything works fine, except the 'Recent posts' and 'Related Posts' functions.

The related posts does it's job well, but shows the title in all languages. Example: English titleFrench titleGerman title.

The Recent post widget shows all languages, so when you're on the English page, it also shows the posts created in other languages. I only want to show the posts that are available in the current language.

I've already tried to wrap the '__()' and '_e()' functions around the title/content to be shown, but no luck.

Thanks!

Answers (4)

2011-10-05

Peter Michael answers:

See if 'Settings->Languages->Hide Untranslated Content' is checked.


priktop comments:

Yes, it is.


Peter Michael comments:

Have you seen 'This function will not work correctly if you installed qTranslate on a blog with existing entries. In this case you will need to take a look at "Convert Database" under "Advanced Settings".' ?


priktop comments:

Yes I have, and qTranslate was installed first thing, so that couldn't be the case :(


Peter Michael comments:

I'm out of ideas, good luck.


priktop comments:

Too bad, thanks anyway!

2011-10-05

Julio Potier answers:

Hello

what's the plugin you're using to show related posts ? Or is a theme function ?

see you


priktop comments:

Theme function, here is the code:



/* ---------------------------------------
RECENT POSTS WIDGET
--------------------------------------- */
class show_recent extends WP_Widget {
function show_recent() {
$widget_ops = array('classname' => 'show_recent', 'description' => __('Show your recent posts.'));
$this->WP_Widget('show_recent', __('CUSTOM - Recent Posts'), $widget_ops);
}

function widget($args, $instance){
extract($args);

//$options = get_option('custom_recent');
$title = $instance['title'];
$posts = $instance['posts'];

//GET the posts
global $post;
$exclude = B_getExcludedCats();
$myposts = get_posts('numberposts='.$posts.'&offset=0&category='.$exclude);

echo $before_widget . $before_title . $title . $after_title;


//SHOW the posts
foreach($myposts as $post){
setup_postdata($post);
//added strip_tags to solve a problem with code being displayed improperly.
?>
<div class="footer_post">
<h4><a href="<?php __(the_permalink()) ?>"><?php __(the_title()); ?></a></h4>
<p><a href="<?php __(the_permalink()) ?>"><?php echo substr(strip_tags(__($post->post_content)), 0, 125); ?>...</a></p>
</div><!-- end footer_post -->
<?php
}
echo $after_widget;
}

function update($newInstance, $oldInstance){
$instance = $oldInstance;
$instance['title'] = strip_tags($newInstance['title']);
$instance['posts'] = $newInstance['posts'];

return $instance;
}

function form($instance){
echo '<p><label for="'.$this->get_field_id('title').'">' . __('Title:') . '</label><input class="widefat" id="'.$this->get_field_id('title').'" name="'.$this->get_field_name('title').'" type="text" value="'.$instance['title'].'" /></p>';

echo '<p><label for="'.$this->get_field_id('posts').'">' . __('Number of Posts:', 'widgets') . '</label><input class="widefat" id="'.$this->get_field_id('posts').'" name="'.$this->get_field_name('posts').'" type="text" value="'.$instance['posts'].'" /></p>';

echo '<input type="hidden" id="custom_recent" name="custom_recent" value="1" />';
}
}


And


/* ----- RELATED POSTS ----- */
function related_posts_shortcode( $atts ) {
extract(shortcode_atts(array(
'title' => '',
'limit' => '5',
), $atts));
global $wpdb, $post, $table_prefix;
if ($post->ID) {
$retval = '[raw]<div class="related_posts"><h4>'.$title.'</h4><ul class="list">[/raw]';
// Get tags
$tags = wp_get_post_tags($post->ID);
$tagsarray = array();
foreach ($tags as $tag) {
$tagsarray[] = $tag->term_id;
}
$tagslist = implode(',', $tagsarray);

// Do the query
$q = "
SELECT p.*, count(tr.object_id) as count
FROM $wpdb->term_taxonomy AS tt, $wpdb->term_relationships AS tr, $wpdb->posts AS p
WHERE tt.taxonomy ='post_tag'
AND tt.term_taxonomy_id = tr.term_taxonomy_id
AND tr.object_id = p.ID
AND tt.term_id IN ($tagslist)
AND p.ID != $post->ID
AND p.post_status = 'publish'
AND p.post_date_gmt < NOW()
GROUP BY tr.object_id
ORDER BY count DESC, p.post_date_gmt DESC
LIMIT $limit;";

$related = $wpdb->get_results($q);
if ( $related ) {
foreach($related as $r) {
$retval .= '<li><a title="'.wptexturize($r->post_title).'" href="'.get_permalink($r->ID).'">'.wptexturize($r->post_title).'</a></li>';}
} else {
$retval .= '<li>No related posts found</li>';}
$retval .= '[raw]</ul></div><!-- end related posts -->[/raw]';
return $retval;
}return;}


Julio Potier comments:

Ok i don't know qTranslate, how this plugin can recognize a FR or EN post ?
thank you

(i cna have a look with an admin acces if you want)


priktop comments:

It fills the DB record with tags, like this:


<!--en-->English text<!--:--><!--nl-->Dutch text<!--:--><!--fr-->French text<!--:-->


Julio Potier comments:

ooooooook... this is absolutely bad, the plugin use a "like" on "post content", bad bad bad.
Anyway.

Try this :
Between this 2 lines in RELATED POSTS :
AND p.post_date_gmt < NOW()
GROUP BY tr.object_id

Add this :
AND p.post_content LIKE '%<!--:" . qtrans_getLanguage() . "-->%'
now you have
AND p.post_date_gmt < NOW()
<strong>AND p.post_content LIKE '%<!--:" . qtrans_getLanguage() . "-->%'</strong>
GROUP BY tr.object_id


If it works, we have to modify the "get_posts" in a sull quey like this one.


priktop comments:

Doesn't work, but I just noticed that it shows the URL of the current language well at the related posts, but it concatenates the title with all languages.... So the title is the only problem.


Julio Potier comments:

ok, can you paste me the title of a post here ?
Thank you
(or grant me access to gain time for both !)


priktop comments:

Okay, check PM.

2011-10-05

jevusi answers:

you can probably use the conditional tag
if(qtrans_getLanguage() == "en")

2011-10-05

Abdessamad Idrissi answers:

First of all it is highly discouraged to query the database directly this way. because if it works now, it won't in the future since WP doesn't keep the same database structure all the time and also WordPress is full on integrated functionality for every specific situation.. so no need for direct DB querying.

The best aproach is to create for every post a separate translation post - just like the way [[LINK href="http://wpml.org/"]]The WordPress Multilingual Plugin[[/LINK]] does.

Did u give it a try?


priktop comments:

Nope, but it's too late for that now.