WPML-按个别语言或所有语言过滤

时间:2018-02-09 作者:Pipo

我在一个多语言网站上有一个基于两个参数的查询(使用WPML)。为了简化,假设我的过滤器之一是要检索的帖子的语言:英语、法语或所有语言。英语和法语都很好,但我不能让“所有语言”过滤器工作。

这是我的代码:

<?php
// First I get the "lang" Get value
$langGet = get_query_var(\'lang\', ICL_LANGUAGE_CODE);
?>

/* Then  have my form to filter posts */
<form action="<?php echo get_permalink( get_the_ID() ); ?>" method="get">
    <label for="lang"><?php _e( \'Languages\', \'mytextdomain\' ); ?></label>
    <?php
    $languages = apply_filters( \'wpml_active_languages\', NULL );
    if($languages) { ?>
        <select name="lang">
            <?php foreach($languages as $language) : ?>
                <option value="<?php echo $language[\'language_code\']; ?>" <?php echo ( $langGet == $language[\'language_code\'] ) ? \'selected\' : \'\'; ?>><?php echo $language[\'translated_name\']; ?></option>
            <?php endforeach; ?>
            <option value="all" <?php echo ( $langGet == \'all\' ) ? \'selected\' : \'\'; ?>><?php _e(\'All languages\', \'mytextdomain\'); ?></option>
        </select>
    <?php } ?>
    <input type="submit" value="<?php _e( \'Filter projects\', \'mytextdomain\' ); ?>">
</form>

<?php
// Then I set up the switch for current language
if($langGet != \'all\') {
    global $sitepress;
    $current_lang = $sitepress->get_current_language();
    $sitepress->switch_lang($langGet, true);
}

// Then I define my query
// Here is the line that is supposed to show all languages but it\'s not working.
$filters = ( $langGet == \'all\' ) ? true : false;

$taxId = apply_filters( \'wpml_object_id\', 755, \'project_category\' );

$paged = (get_query_var(\'paged\')) ? absint(get_query_var(\'paged\')) : 1;

$args = array(
    \'posts_per_page\' => 16,
    \'post_type\' => \'projects\',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'project_category\',
            \'field\' => \'term_id\',
            \'terms\' => $taxId,
        )
    ),
    \'orderby\'   => \'date\',
    \'order\' => \'ASC\',
    \'paged\' => $paged,
    \'suppress_filters\' => $filters
);
$projects = new WP_Query($args); 
但这并不是显示所有语言。此外,我注意到#038;lang=all 当我在第2页时,添加到url的末尾。

有人能帮我吗?谢谢

2 个回复
最合适的回答,由SO网友:Pipo 整理而成

所以我成功了。问题不在于过滤器,而在于税务查询。以下是可能感兴趣的人的解决方案。

<?php
// I check the lang query var if it\'s empty we\'ll display all languages
$langGet = get_query_var(\'lang\', \'all\');
?>

/* Then  have my form to filter posts */
<form action="<?php echo get_permalink( get_the_ID() ); ?>" method="get">
    <label for="lang"><?php _e( \'Languages\', \'mytextdomain\' ); ?></label>
    <?php
    $languages = apply_filters( \'wpml_active_languages\', NULL );
    if($languages) { ?>
        <select name="lang">
            <?php foreach($languages as $language) : ?>
                <option value="<?php echo $language[\'language_code\']; ?>" <?php echo ( $langGet == $language[\'language_code\'] ) ? \'selected\' : \'\'; ?>><?php echo $language[\'translated_name\']; ?></option>
            <?php endforeach; ?>
            <option value="all" <?php echo ( $langGet == \'all\' ) ? \'selected\' : \'\'; ?>><?php _e(\'All languages\', \'mytextdomain\'); ?></option>
        </select>
    <?php } ?>
    <input type="submit" value="<?php _e( \'Filter projects\', \'mytextdomain\' ); ?>">
</form>

<?php
// Then I set up the switch for current language
if( $langGet != \'all\' ) {
    global $sitepress;
    $current_lang = $sitepress->get_current_language();
    $sitepress->switch_lang($langGet, true);
    $taxTerms = array( apply_filters( \'wpml_object_id\', 755, \'project_category\' ) );
    $filters = false;
} else {
    // If we want to display all languages we have to remove filters
    global $sitepress;
    remove_filter( \'get_terms_args\', array( $sitepress, \'get_terms_args_filter\' ), 10 );
    remove_filter( \'get_term\', array( $sitepress, \'get_term_adjust_id\' ), 1 );
    remove_filter( \'terms_clauses\', array( $sitepress, \'terms_clauses\' ), 10 );
    $taxTerms = array( 755, 776, 777, 778 );
    $filters = true;
}

$paged = (get_query_var(\'paged\')) ? absint(get_query_var(\'paged\')) : 1;

// Then we set the query
$args = array(
    \'posts_per_page\' => $postsperpage,
    \'post_type\' => \'projects\',
    \'tax_query\' => array(
      array(
          \'taxonomy\' => \'project_category\',
          \'field\' => \'term_id\',
          \'terms\' => $taxTerms
      )
    ),
    \'orderby\'   => $orderby,
    \'order\' => $order,
    \'meta_key\' => $metakey,
    \'paged\' => $paged,
    \'suppress_filters\' => $filters
);
$projects = new WP_Query($args); 

结束

相关推荐