• I looked and did not see any topics that covered this.

    I am using $api->get_translations($translations_args) that I’ve found in some of your documentation in order to build a custom language selector that will work with a custom theme.

    All is going well, but I cannot find anything on sorting what is returned in any of the documents that I’ve found.

    Are there any parameters that I can include in $translations_args that will return the translations in a specific order or do I need to figure out how to order them after I get the list?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support dinamiko

    (@dinamiko)

    Hi John,

    There are not sorting paramenter in translation arguments, however you can use standard PHP array functions to manipulate the result, for example array_reverse:

    $items = array_reverse( mlp_custom_get_language_items() );

    /**
     * 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

    Thread Starter John Huebner

    (@hube2)

    Thanks, that’s all I needed to know. I don’t have a problem building something to sort the but I didn’t want to build something if it already existed.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘get_translations sorting’ is closed to new replies.