Return to Snippet

Revision: 65186
at November 30, 2016 18:28 by kkarpieszuk


Updated Code
<?php

// save current language
$current_lang = apply_filters( 'wpml_current_language', null );

//get the default language
$default_lang = apply_filters( 'wpml_default_language', null );
//fetch posts in default language
do_action( 'wpml_switch_language', $default_lang);
//query args
$custom_query_args = array(
    'cat' => 1
);
//build query
$custom_query = new wp_query($custom_query_args);
//loop
while ( $custom_query->have_posts() ) : $custom_query->the_post();
    //check if a translation exist
    $t_post_id = apply_filters( 'wpml_object_id', $post->ID, 'post', false, $current_lang );
    if(!is_null($t_post_id)){
        $t_post = get_post( $t_post_id);
    ?>
        <a href="<?php echo get_permalink($t_post_id); ?>" title="<?php echo get_the_title($t_post_id); ?>"><?php echo get_the_title($t_post_id); ?></a>
        <?php
    }
    //no translation? display default language
    else{ ?>
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
        <?php
    }  
endwhile;
wp_reset_query();
do_action( 'wpml_switch_language', $current_lang);

Revision: 65185
at November 6, 2013 06:58 by kkarpieszuk


Initial Code
<?php
 global $sitepress;
// save current language
$current_lang = $sitepress->get_current_language();
//get the default language
$default_lang = $sitepress->get_default_language();
//fetch posts in default language
$sitepress->switch_lang($default_lang);
//query args
$custom_query_args = array(
    'cat' => 1
);
//build query
$custom_query = new wp_query($custom_query_args);
//loop
while ( $custom_query->have_posts() ) : $custom_query->the_post();
    //check if a translation exist
    $t_post_id = icl_object_id($post->ID, 'post', false, $current_lang);
    if(!is_null($t_post_id)){
        $t_post = get_post( $t_post_id);
    ?>
        <a href="<?php echo get_permalink($t_post_id); ?>" title="<?php echo get_the_title($t_post_id); ?>"><?php echo get_the_title($t_post_id); ?></a>
        <?php
    }
    //no translation? display default language
    else{ ?>
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
        <?php
    }  
endwhile;
wp_reset_query();
$sitepress->switch_lang($current_lang);

Initial URL

                                

Initial Description
shows list of cpt for example on archive page when wpml is installed. if particular post has no translation, it shows it in default language

Initial Title
wpml - show all CPT at archive page, default language if not translated

Initial Tags
wordpress

Initial Language
PHP