Support Log in

10. Template Functions

Developer Guide
Prefer video? Watch the Lang Forge tutorials 9 short guides covering every feature Watch

Common patterns for building multilingual theme templates with Lang Forge. These examples demonstrate how to handle language-specific layouts, conditional content, menus, archives, and more.

10.1 Language-Aware Template Logic

php
// header.php - Load language-specific header partial
$lang = lf_get_current_language();
$header_file = get_template_directory() . '/partials/header-' . $lang . '.php';

if (file_exists($header_file)) {
    include $header_file;
} else {
    include get_template_directory() . '/partials/header-default.php';
}

10.2 Language-Specific Menu Locations

php
// functions.php - Register separate menu locations per language
add_action('after_setup_theme', function () {
    $languages = function_exists('lf_get_languages') ? lf_get_languages() : [];

    register_nav_menus(['primary' => 'Primary Menu']);
    register_nav_menus(['footer'  => 'Footer Menu']);

    foreach ($languages as $code => $lang) {
        register_nav_menus([
            'primary_' . $code => 'Primary Menu (' . $lang['name'] . ')',
            'footer_' . $code  => 'Footer Menu (' . $lang['name'] . ')',
        ]);
    }
});

// header.php - Display the correct menu
$lang = lf_get_current_language();
$menu_location = 'primary_' . $lang;

if (has_nav_menu($menu_location)) {
    wp_nav_menu(['theme_location' => $menu_location]);
} else {
    wp_nav_menu(['theme_location' => 'primary']);
}
Example: Reusable function for language-aware menus
php
function theme_nav_menu($base_location, $args = []) {
    $lang = lf_get_current_language();
    $lang_location = $base_location . '_' . $lang;

    $defaults = [
        'theme_location'  => has_nav_menu($lang_location) ? $lang_location : $base_location,
        'container'       => 'nav',
        'container_class' => 'navigation-' . $base_location,
    ];

    wp_nav_menu(array_merge($defaults, $args));
}

// Usage in templates:
theme_nav_menu('primary', ['menu_class' => 'main-menu']);
theme_nav_menu('footer', ['menu_class' => 'footer-menu', 'depth' => 1]);

10.3 Translated Front Page ID

Lang Forge filters option_page_on_front so that is_front_page(), menu active states, and breadcrumbs all resolve correctly for translated front pages. No additional code is required in your theme.

php
// This works correctly in any language
if (is_front_page()) {
    get_template_part('template-parts/front-page');
}

// If you need the actual translated front page ID:
$front_page_id = get_option('page_on_front');
// This already returns the translated ID thanks to Lang Forge's filter

10.4 Search Form with Language Context

php
// searchform.php
$lang = lf_get_current_language();
?>
<form role="search" method="get" class="search-form" action="<?php echo esc_url(home_url('/')); ?>">
    <label>
        <span class="screen-reader-text">
            <?php _e('Search for:', 'theme'); ?>
        </span>
        <input type="search"
               class="search-field"
               placeholder="<?php echo esc_attr__('Search...', 'theme'); ?>"
               value="<?php echo get_search_query(); ?>"
               name="s">
    </label>
    <button type="submit" class="search-submit">
        <?php _e('Search', 'theme'); ?>
    </button>
</form>

> Note: Lang Forge automatically adds the language parameter to search results so only posts in the current language are shown. You do not need to add a hidden language field.

Forge AI Assistant Online

Hi! I'm the Lang Forge AI assistant. Ask me anything about the plugin — setup, features, troubleshooting, or development.

Just now
Powered by Forge AI · Browse docs