Polylang compatibility
-
Hi!
Thank you for this awesome plugin for blog promotion!
But there is an issue I’ve faced – pushing translated posts to the related ones.The thing is normally you won’t face it because languages are not similar, but the Slavic group languages are. So that’s why I get the same post related but on other language.
Maybe you could add some setting in the update to avoid it?
It could be an option similar to “same author” or “similar post type”.
It’s just another taxonomy at least and I hope it wont take a lot of time for you.Many thanks!
I really appreciate that!
-
The plugin already implements basic PolyLang implementation.
https://github.com/WebberZone/contextual-related-posts/blob/v2.9.4/includes/i10n.php#L48
I don’t use the plugin myself so quite difficult for me to test. Is your installation different from the way Polylang generates its posts?
I’ve got basic polylang and CRP plugins code – nothing is changed.
It seems the bug appears when the post is sticked.
I have 2 sticked posts in two languages and one of them shows translation as a related post.Maybe this information could help you to solve the issue?
Thanks.-
This reply was modified 4 years, 5 months ago by
Nikita_Sp.
Sorry for the delayed response. I’ll make a note of it, but I don’t use polyLang and it’s very hard for me to debug as a result.
I’m also not sure what code change needs to be done in order to get it to work to be honest.
@ajay well just create some new website with default theme. Add polylang from plugin installer and install your plugin.
Add two languages for tests. Add a sticky post for each language with similar title.
It’d takes around 30 minutes.Than you can edit your code and debug to find out the error.
I hope it helps.i checked the link you sent me but I’m confused now as I can’t see the contextual related posts code in there at all. I do see related posts but not from my plugin
@ajay it’s placed on the right column on desktop version. It has grey background. We use custom template for that, not just a shortcode.
What is the code you’re using for those? Given it is a custom install it might require some extra checking as the Polylang implementation happens when the plugin generates the output and not when it returns the posts from the DB.
@ajay I use
echo_crp()
function andadd_filter('crp_custom_template', 'crp_template', 10, 3);
to output the posts.The template function is quite simple (taken from documentation):
function crp_template($null, $posts, $plugin_settings){ $html = ''; $posts = array_slice($posts, 0, 3); foreach($post as $post) ... return $html; }
And files of the plugin didn’t change.
So our code is not impact on the plugin algorithms.Thanks.
Thanks – that makes sense. within your foreach, please can you call this function and the rest of the code.
crp_object_id_cur_lang( $post->ID );
@ajay seems I’m doing something wrong because the result after adding this code is post from another language only.
Here is a function code:
add_filter('crp_custom_template', 'crp_template', 10, 3); function crp_template($null, $posts, $plugin_settings){ $html = ''; $posts = array_slice($posts, 0, 3); if($posts){ $html = "<div class=\"sidebar-block related-posts\">"; // We need this for WPML support. $processed_results = array(); foreach ($posts as $post) { /* Support WPML */ $resultid = crp_object_id_cur_lang( $post->ID ); // If this is NULL or already processed ID or matches current post then skip processing this loop. if ( ! $resultid || in_array( $resultid, $processed_results ) || intval( $resultid ) === intval( $post->ID ) ) { // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict continue; } // Push the current ID into the array to ensure we're not repeating it. array_push($processed_results, $resultid); $html .= " <div class=\"related-post\">"; $html .= " <span class=\"post-data post-meta\" >"; $html .= " <a href=\"".get_permalink( $post->ID )."\" class=\"title\" >".get_the_title($post->ID)."</a>"; $html .= " <span class=\"published\">". get_the_date('d.m.Y', $post->ID) ."</span>"; $html .= " </span>"; $html .= " </div>"; } $html .= "</div>"; } return $html; }
I think the error is about using $post variable (but I’m obvious could mistaken).
Thanks.Instead of using $posts / $post – I think $post might be reserved. What if you just use $results and $result like I did?
@ajay I’ve just tried to rename all the variables. The result is the same – just translated post got to the list.
The thing is the function got the list of posts from functions inside the plugin.
This function is just an output code – it’s not resolving the relation.Of course we can exclude it from the list in the cycle, but it’s a dirty solution.
Thanks.
I’m not sure to be frank. Can you please try the automatic option and see what gets displayed with the plugins inbuilt functions? Are you having the same issue?
And, is it only for the sticky posts that you see the problem@ajay interesting, when I disable the output filter (function for updating the template)
add_filter('crp_custom_template', 'custom_crp_template', 10, 3);
the issue is disappearing.When I add the function and just do
print_r()
the translated post’s ID is in the array:function custom_crp_template($null, $results, $plugin_settings){ print_r($results); // there is an ID of the translated post version in array }
Seems you have different ways to create the array. Default template has no this ID, but the custom – has.
Seems like there is a code that remove the translated version, but when I use custom template – it’s not executing. Why? The thing is the code should create the data first, and than work about the output.
Now, while writing this answer, I’m looking through your code and see what is causing the issue. File
include/main-query.php
on line 116 you add custom template support and remove the translated posts on line 155-164 directly in the loop of output.You should add this exception BEFORE the custom template filter. And prepare the related posts array before ALL the templates.
If I put that code 155-164 to the
function custom_crp_template($null, $results, $plugin_settings){
it won’t work. I need to add alsoglobal $post
to the function, and the amount of posts will be different because I need exactly 3 posts and I slice the array to 3 elements. If one of the elements in the loop will be unwanted – I need another one.Sorry for my English, I hope you’ll get what I mean.
I understand you need to change your code to resolve this issue, but it would be correct from a programming point of view and I’ll rate it 5 stars!Thanks.
-
This reply was modified 4 years, 5 months ago by
- The topic ‘Polylang compatibility’ is closed to new replies.