• Resolved serhatevren

    (@serhatevren)


    Hi Guys,

    This problem was big headache for me but ı find a trick way finally..

    // posts array
    			$posts_data = array();
    $countpost = 0;
    			if ( !is_array($mostpopular) || empty($mostpopular) ) { // no posts to show
    				$content .= "<p>".__('Sorry. No data so far.', 'wordpress-popular-posts')."</p>"."\n";
    			} else { // list posts
    
    				// HTML wrapper
    				if ($instance['markup']['custom_html']) {
    					$content .= htmlspecialchars_decode($instance['markup']['wpp-start'], ENT_QUOTES) ."\n";
    				} else {
    					$content .= "<ul class=\"wpp-list\">" . "\n";
    				}
    
    				foreach($mostpopular as $p) {
    					$countpost++;
    					$stats = "";
    					$thumb = "";
    					$title = "";
    					$title_sub = "";
    					/** WPML COMPATIBLE **/
    					global $sitepress;
    
    					if (ICL_LANGUAGE_CODE==en) {
    
    					$current_main_id = icl_object_id( $p->id, 'post', true, $sitepress->get_default_language() );
    					$current_slug = get_post( $current_main_id );
    					$slug = $current_slug->post_name;
    					}
    					else {
    					$current_main_id = icl_object_id( $p->id, 'post', true, 'ko' );
    					$current_slug = get_post( $current_main_id );
    					$slug = $current_slug->post_name;
    					}
    
    					$permalink = get_permalink( $current_main_id);
    					$author = ($instance['stats_tag']['author']) ? get_the_author_meta('display_name', $p->uid) : "";
    					$date = date_i18n( $instance['stats_tag']['date']['format'], strtotime($p->date) );
    					$pageviews = ($instance['order_by'] == "views" || $instance['order_by'] == "avg" || $instance['stats_tag']['views']) ? (($instance['order_by'] == "views" || $instance['order_by'] == "comments") ? $p->pageviews : $p->avg_views ) : 0;
    					$comments = ($instance['order_by'] == "comments" || $instance['stats_tag']['comment_count']) ? $p->comment_count : 0;
    
    					$post_cat = "";
    					$excerpt = "";
    					$rating = "";
    					$data = array();
    
    					// TITLE
    					$title = ($this->qTrans) ? qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($p->title) : $p->title;
    
    					$title = strip_tags(get_the_title($current_main_id));
    					$title_sub = strip_tags($title);
    
    					// truncate title
    					if ( $instance['shorten_title']['active'] ) {
    
    						if ( isset($instance['shorten_title']['words']) && $instance['shorten_title']['words'] ) { // by words
    
    							$words = explode(" ", $title, $instance['shorten_title']['length'] + 1);
    
    							if ( count($words) > $instance['shorten_title']['length'] ) {
    
    								array_pop($words);
    								$title_sub = implode(" ", $words) . "...";
    
    							}
    
    						} else { // by characters
    
    							if ( strlen($title) > $instance['shorten_title']['length'] ) {
    								$title_sub = mb_substr($title, 0, $instance['shorten_title']['length'], $this->charset) . "...";
    							}
    
    						}
    
    					}
    
    					$title = htmlspecialchars( $title, ENT_QUOTES, $this->charset );
    					$title_sub = htmlspecialchars( $title_sub, ENT_QUOTES, $this->charset );
    
    					$title = apply_filters('the_title', $title, $p->id);
    					$title_sub = apply_filters('the_title', $title_sub, $p->id);

    But this is just trick if current language is EN,
    I can change title and link with current language..

    Anyway ı hope this can be useful for you.
    I hope plugin owner will make this plugin compatible with WPML..

    THANKS.

    SERHAT EVREN

    https://wordpress.org/plugins/wordpress-popular-posts/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi Serhat,

    Thanks for sharing this piece of code! I’m sure others will appreciate it, I don’t use WPML nowadays. None the less, I’ll test this code once I get some spare time and see if I can improve it.

    Hi Hector,

    You forgot to add the fix Serhat provided to the _get_thumb function.
    That causes to have different permalinks on titles and on thumbs when showing Popular Posts.

    Also, it would be nice if I would have the German Top Posts when I’m on /de/ … So Popular Posts based on language.

    And another thing.
    I can live with the fact that there are english posts shown in the Most Popular list.
    The fact is that if a post from that list is translated into German (and not published – so a draft post) – you still show it and link it to /?p=283 and users that are not logged in will get a 404 error.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘WPML Trick’ is closed to new replies.