Skip to content

Instantly share code, notes, and snippets.

@Faisalawanisee
Last active February 10, 2019 21:55
Show Gist options
  • Save Faisalawanisee/1d1b81373bc160c104e7 to your computer and use it in GitHub Desktop.
Save Faisalawanisee/1d1b81373bc160c104e7 to your computer and use it in GitHub Desktop.
WordPress Multilingual Menu
<?php
$fr_page = get_field('fr_page', 'options');
// Set up the objects needed
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'posts_per_page' => -1));
$new_wp_query = new WP_Query();
$all_french_posts = $new_wp_query->query(
array(
'post_type' => array('post', 'service'),
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'select_language',
'value' => 'English',
),
array(
'key' => 'english_page_select',
),
),
));
$sizeOfall_french_posts = sizeof($all_french_posts);
$fr_posts = array();
for ($i=0; $i < $sizeOfall_french_posts; $i++) {
$fr_posts[] = $all_french_posts[$i]->ID;
}
// Filter through all pages and find Portfolio's children
$fr_childrens_data = get_page_children( $fr_page, $all_wp_pages );
$sizeOfChildrens = sizeof($fr_childrens_data);
$fr_pages = array();
for ($i=0; $i < $sizeOfChildrens; $i++) {
$fr_pages[] = $fr_childrens_data[$i]->ID;
}
array_push($fr_pages, $fr_page);
$fr_posts_pages = array_merge($fr_posts, $fr_pages);
$french_page_select = get_field('french_page_select');
$english_page_select = get_field('english_page_select');
if($french_page_select || $fr_posts_pages): ?>
<ul class="language-list">
<li class="<?php if(!in_array(get_the_id(), $fr_posts_pages) ){ echo 'active'; } ?>">
<?php if(!in_array(get_the_id(), $fr_posts_pages)): ?>
<?php echo __('EN', 'qaonrequest') ?>
<?php else: ?>
<a href="<?php echo get_permalink($english_page_select); ?>"><?php echo __('EN', 'qaonrequest') ?></a>
<?php endif; ?>
</li>
<li class="<?php if(in_array(get_the_id(), $fr_posts_pages) ){ echo 'active'; } ?>">
<?php if(in_array(get_the_id(), $fr_posts_pages)): ?>
<?php echo __('FR', 'qaonrequest') ?>
<?php else: ?>
<a href="<?php echo get_permalink($french_page_select); ?>"><?php echo __('FR', 'qaonrequest') ?></a>
<?php endif; ?>
</li>
</ul>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment