• Resolved janusch

    (@janusch)


    There appears to be a bug with multilingualpress, as there is a function called mlp_get_interlinked_permalinks which is used to create custom language switchers. It returns empty array on the blog page an one of the category-archives, although it works well everywhere else, returning permalinks to translations and language codes. Now, whats interesting, there is another similar function mlp_get_linked_elements which returns a simple array with just network site id and page/post id and this one returns ids of translations properly, also for the blog page.

    • This topic was modified 7 years, 4 months ago by janusch.
    • This topic was modified 7 years, 4 months ago by janusch.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi there,

    assuming you have your front page set to display your latest posts, both mlp_get_interlinked_permalinks() and mlp_get_linked_elements() should return an empty array, and they do for me.

    This is because there are no elements, and the front page itself is not based off some element, too. Linked elements can be posts (or pages or attachments), or term archives (no post type archives, because, again, there is no element).

    If you want to create some custom language switcher (why?), you should use either mlp_show_linked_elements() and configure it according to your liking, or, on a low level, query the translations directly off the Language API (which you can get via the mlp_language_api filter).

    Hope this helps.

    Cheers,
    Thorsten

    Thread Starter janusch

    (@janusch)

    Hi Thorsten,

    did you open the page (I provided the link)? We do not have front page set to display latest posts. Our front page and blog page are separate pages and the blog pages have been interlinked accordingly.

    The proposed solution is somehow irritating, because we have created the language switcher in accordance with your own tutorial, which specifically tells to use that mlp_get_interlinked_permalinks() function. Is this tutorial obsolete?

    Cheers,
    Janusch

    Plugin Support dinamiko

    (@dinamiko)

    Hi janusch,

    Regarding custom language switcher, here is an example that creates a form select and adds it to the sidebar, you can adapt the code to your needs (use a list instead of select…), the important function here is mlp_custom_get_language_items which returns an array with all the items you need:

    // Add select to sidebar
    add_action( 'mlp_and_wp_loaded', function() {
    	add_action( 'get_sidebar', 'mlp_custom_get_dropdown_menu' );
    });
    
    // create select
    function mlp_custom_get_dropdown_menu() {
    
    	$items = array_reverse( mlp_custom_get_language_items() );
    	?>
    	<select onchange="this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);">
    		<?php //var_dump($items);?>
    		<?php foreach ( $items as $item ) { ?>
    			<option <?php echo esc_attr( $item['active'] ) == true ? 'selected="selected"' : '';?> value="<?php echo esc_url( $item['url'] );?>"><?php echo esc_attr( $item['name'] );?></option>
    		<?php } ?>
    	</select>
    	<?php
    }
    
    /**
     * Get language items.
     *
     * @return array|void
     */
    function mlp_custom_get_language_items() {
    
    	$api = apply_filters( 'mlp_language_api', NULL );
    	if ( ! is_a( $api, 'Mlp_Language_Api_Interface' ) ) {
    		return;
    	}
    
    	/**
    	 * @type int    $site_id      Base site
    	 * @type int    $content_id   post or term_taxonomy ID, *not* term ID
    	 * @type string $type         @see Mlp_Language_Api::get_request_type()
    	 * @type bool   $strict       When TRUE (default) only matching exact
    	 *                                 translations will be included
    	 * @type string $search_term  If you want to translate a search
    	 * @type string $post_type    For post type archives
    	 * @type bool   $include_base Include the base site in returned list
    	 */
    	$translations_args = array(
    		'strict'       => FALSE,
    		'include_base' => TRUE,
    	);
    	$translations      = $api->get_translations( $translations_args );
    	if ( empty( $translations ) ) {
    		return;
    	}
    
    	$items = array();
    	/** @var Mlp_Translation_Interface $translation */
    	foreach ( $translations as $site_id => $translation ) {
    
    		$url = $translation->get_remote_url();
    		if ( empty( $url ) ) {
    			continue;
    		}
    
    		$language = $translation->get_language();
    		$active   = FALSE;
    		if ( get_current_blog_id() === $site_id ) {
    			$active = TRUE;
    		}
    
    		$items[ $site_id ] = array(
    			'url'    => $url,
    			'http'   => $language->get_name( 'http' ),
    			'name'   => $language->get_name( 'native' ),
    			'active' => $active,
    		);
    	}
    
    	return $items;
    }

    Thanks,
    Emili

    • This reply was modified 7 years, 4 months ago by dinamiko.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Bug in mlp_get_interlinked_permalinks’ is closed to new replies.