0

我想改进一个 Wordpress 插件,它结合 WPML 向菜单添加一个或多个标志。(插件名称:菜单中的 WPML 标志)它应该只适用于主要位置的菜单,而不适用于侧边栏或页脚等其他位置。

<?php
/*
 Plugin Name: WPML flag in menu
 Plugin URI: http://www.MijnPress.nl
 Description: Shows translated flags (for every language except current viewing lang) in the default or wp_nav_menu at last position
 Version: 1.1
 Author: Ramon Fincken
 Author URI: http://www.MijnPress.nl
 */

add_filter( 'wp_nav_menu_items', 'plugin_wpml_flag_in_menu' );

function plugin_wpml_flag_in_menu($items, $args = NULL)
{
    if(function_exists('icl_get_languages'))
    {
        $languages = icl_get_languages('skip_missing=0&orderby=code');
        $new_items = '';
        if(!empty($languages)){
            foreach($languages as $l){
                // Exclude current viewing language             
                if($l['language_code'] != ICL_LANGUAGE_CODE)
                {
                    $new_items .= '<li class="menu-item menu-item-type-post_type menu-item-object-page">';
                    if(!$l['active']) $new_items .= '<a href="'.$l['url'].'">';
                    if($l['country_flag_url']){
                        $new_items .= '<img src="'.$l['country_flag_url'].'" height="12" alt="'.$l['language_code'].'" width="18" />';
                    }
                    // $items .= icl_disp_language($l['native_name'], $l['translated_name']);
                    if(!$l['active']) $new_items .= '</a>';
                    $new_items .= '</li>';
                }
            }
        }
    }
    // Idea by Simon Weil
    if(is_rtl())
    {
        $items = $new_items.$items;
    }
    else
    {
        $items .= $new_items;
    }

    return $items;
}
?>

我想用“if(function_exists('icl_get_languages') && $location == 'primary')”之类的东西来扩展if语句(“if(function_exists('icl_get_languages'))”,但我不知道如何得到“当前位置”的值。

4

1 回答 1

0

theme_location可通过以下方式获得$args

add_filter( 'wp_nav_menu_items', 'plugin_wpml_flag_in_menu', 10, 2 );
function plugin_wpml_flag_in_menu( $items, $args ) {
    if( $args->theme_location == 'primary' ) {
        // do stuff
    }
    return $items;
}
于 2013-06-01T17:45:50.670 回答