Top 10 and languages in WPML
-
Hi, in widget Top 10 shows me the same post in two languages (PL, EN). Post have the same number of visits. Is it possible to show posts depends on language?
-
Hi,
I’m not sure how WPML works in terms of pulling out the correct post language.
Do you have an insight on this and maybe I can give a suggestion on pulling this out.
this is my test website, i have newest wpln on it. What do you need? Acces to my website panel or something?
Hi,
I need some guidance on how WPML displays it’s various posts. Do you have documentation on this or can you check with their team on how to pull this out.
I don’t need access to the site.
http://wpml.org/documentation/support/wpml-coding-api/
i think it is what we need, but i don’t know how to use it with your plugin
icl_object_id(ID, type, return_original_if_missing, language_code)Hi,
This hasn’t been tested, but you could add a function to your functions.php which will attempt to overwrite the ID being fetched.
function filter_tptn_post_id( $ID ) { return icl_object_id( $ID ); } apply_filters( 'tptn_post_id' , 'filter_tptn_post_id' );
I tried the suggested function but it did not work for me. I expected to have the same top 10 regardless of language.
So, instead, I opted to add the following function at the end of top-10.php.
/** * Returns the object identifier for the current language (WPML). */ function tptn_object_id_cur_lang( $post_id ) { if ( function_exists( 'icl_object_id' ) ) { return icl_object_id( $post_id, 'any', true, ICL_LANGUAGE_CODE ); } else { return $post_id; } }
And replace the line that gets the $postid as indicated below.
/** * Filter the post ID for each result. Allows a custom function to hook in and change the ID if needed. * * @since 1.9.8.5 * * @param int $result->ID ID of the post */ $postid = tptn_object_id_cur_lang( apply_filters( 'tptn_post_id', $result->ID ) );
It would be great if you can find a way to include such a fix in the plugin to add support for WPML. Perhaps not exactly as I implemented it, but instead something that would be more generic.
I actually had to change things a bit. instead of just replacing the $postid I had to create a new variable called $resultid as follow just before getting the post itself.
$resultid = tptn_object_id_cur_lang( $result->ID );
And then replace all instances of
$result->ID
.So, I reverted back the line above modifying the
$postid
.I also had to add logic to remove duplicate entries. So, if I already had a link I would ignore it.
Here’s the snippet.
$processed_results = array(); foreach ( $results as $result ) { $resultid = tptn_object_id_cur_lang( $result->ID ); if ( in_array( $resultid, $processed_results ) ) { continue; } array_push ( $processed_results, $resultid ); $sumcount = $result->sumCount;
Tony, thanks for the code.
Did you modify top-10.php file to achieve the above in your second post? Which lines exactly?
Tony, this works, thanks!
Ajay:
Put this code at the bottom of the file (or wherever you like based on the structure):
/** * Returns the object identifier for the current language (WPML). */ function tptn_object_id_cur_lang( $post_id ) { if ( function_exists( 'icl_object_id' ) ) { return icl_object_id( $post_id, 'any', true, ICL_LANGUAGE_CODE ); } else { return $post_id; } }
Then search for this line (around 750):
foreach ( $results as $result ) {
Replace that line with the code from Tony, up until the line
$sumcount = $result->sumCount
Thanks jstand for confirming. I’ll put this code into Top-10
Hi,
I’ve implemented this code in v2.1.0 that I released today. Would you be able to confirm it’s working fine when you upgrade?
- The topic ‘Top 10 and languages in WPML’ is closed to new replies.