-
AuthorPosts
-
August 15, 2019 at 8:29 am #1127845
This is not a support request, but rather just a solution to a question that a number of people have asked
I searched this forum high and low for a solution for replacing the main menu with an alternative menu created here: Appearance -> Menus, however all the solutions that I found either recommend using a Multi Language plugin or using the widget area in the Header. However neither of these solutions were ideal for me as I didn’t want the bloat and maintenance issues that come with these sophisticated plugins. So I eventually managed to cobble together the following solution which will allow you to pick an alternative template when building a page, which will in turn assign an alternative header file (which you can customize for your alternative language settings (SEO) and an alternative main menu for all your language specific pages:
Step 1
Copy template-builder.php (or whichever template you use to build your pages) to your child theme folder and rename it to whatever makes sense for your alternative template. I called mine: template-french.php
Copy header.php to your child theme folder and rename it. I called mine: header-french.php
Step 2
Open template-french.php and replace the following line of code on line 24:
get_header();
with
get_header('french');
Note: You only include the part that you added after the header-. This will call your custom header template now instead of the default header in case you want to customize the page head code to include language or geographic specific elements for your SEO. If you don’t want to change the head code, you can skip step 2 entirely.
If you copied the template-builder.php file, you’ll need to add the following code just after the opening php tag at the top of the file:
/* Template Name: French Website */
And if you copied one of the other templates, then just give the template a new name on line 3.
Step 3
Go to Appearance -> Menus and create your alternative menu and populate it with the pages you want this menu to link to. Give it a name that makes sense. I called mine: French Main Menu
Step 4
Add the following code to your functions.php file which will check for your alternative template on the current page and if it finds it, it will display your alternative menu:
/* Display a different menu for French Website */ add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' ); function my_wp_nav_menu_args( $args = '' ) { if($args['theme_location'] === 'avia') if( basename( get_page_template() ) == 'template-french.php') { $args['menu'] = 'French Main Menu'; } else { $args['menu'] = 'Main Menu'; } return $args; }
Replace template-french.php in the above code with your alternative template file name, and “French Main Menu” with the name of your alternative menu, and “Main Menu” with the name of your default main menu.
Step 5
When creating a page that you want to use your alternative menu on, select your alternative template from the “Template” section under “Page Attributes”.
Note: While my goal was language focused, you can use this same solution any time you want to replace the main menu with a different menu on your website.
I hope this helps… and if any of the moderators can suggest improvements on this solution or a more efficient way of doing this, please do let me know.
Thank you.
Brendon
- This topic was modified 5 years, 4 months ago by inMotionGraphics.
August 16, 2019 at 4:07 am #1128123Hey Brendon,
Thanks a lot for sharing this, it’s much appreciated and this will hopefully help someone on the forum in the future :-)
Best regards,
RikardAugust 16, 2019 at 4:24 am #1128128My pleasure Rikard. And yes, I hope this can save someone a lot of time and effort looking for a menu replacement solution, or having to resort to a heavy Multi-lingual plugin that may contain lots of features that they don’t actually need.
August 16, 2019 at 8:33 pm #1128357Hi,
Glad we could help!
Please take a moment to review our theme and show your support https://themeforest.net/downloads
Please bookmark Enfold Documentation for future reference.
Thank you for using Enfold :)Best regards,
BasilisSeptember 10, 2019 at 3:25 pm #1136574Hi,
Thanks for sharing this solution for making a site multilingual, without fancy plugins.
one question though. How would I go about making a site for 3 languages?
note: newbe to Enfold. Basic programming skills
warm regards
PeterSeptember 10, 2019 at 6:10 pm #1136662Hi Peter,
I’m glad to hear you found my solution helpful.
To answer your question, you can expand this solution to accommodate as many languages or alternative menus as you like by replicating steps 1 – 3, and in step 4 expand the code in your functions.php like this:
/* Display multiple alternative menus */ add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' ); function my_wp_nav_menu_args( $args = '' ) { if($args['theme_location'] === 'avia') if( basename( get_page_template() ) == 'template-french.php') { $args['menu'] = 'French Main Menu'; } elseif( basename( get_page_template() ) == 'template-german.php') { $args['menu'] = 'German Main Menu'; } elseif( basename( get_page_template() ) == 'template-italian.php') { $args['menu'] = 'Italian Main Menu'; } else { $args['menu'] = 'Main Menu'; } return $args; }
I haven’t tested the above code, but it should work. If you have any problems getting it to work, let me know.
Note to Moderators: You can leave this thread open in case others have questions. I’m happy to assist with implementation where I can…
September 11, 2019 at 8:37 am #1136929Hi inMotionGraphics,
Thanks a lot for sharing, it’s much appreciated :-)
Best regards,
RikardOctober 8, 2019 at 7:22 am #1146036Hi guys, I’ve run into a small, but frustrating issue with my above outlined solution, in that on the pages that use my French Template (see above for how I created it), some of the Avia Layout Builder elements are adding an additional line break below them in the output HTML source code, which is of course causing my page layout to be incorrect and different from my English site. In the screenshot below you can see the first column is the way it should be, and the second column is what it looks like with the added line break:
Here is how the output source code differs between the two columns in the screenshot above:
Left Column:
<span class='hr-inner-style'></span></span></div><section class="av_textblock_section " itemscope="itemscope" itemtype="https://schema.org/CreativeWork" ><div class='avia_textblock text-justify ' itemprop="text" ><p>Ces deux îles sont étonnamment...
Right Column (note the line break tag before the section tag):
<span class='hr-inner-style'></span></span></div><br /> <section class="av_textblock_section " itemscope="itemscope" itemtype="https://schema.org/CreativeWork" ><div class='avia_textblock text-justify ' itemprop="text" ><p>Les îles vierges...
I’ve manged to narrow down the issue to the line spaces in the Layout Builder’s short code editor field. If I remove all the line breaks between the line space shortcode and the preceding image shortcode, and do the same for the text box shortcode that follows, then the additional line break is not added to the frontend page.
So this:
...Antigua & Barbuda</a> [/av_image] [av_hr class='invisible' height='5' shadow='no-shadow' position='center' custom_border='av-border-thin' custom_width='50px' custom_border_color='' custom_margin_top='30px' custom_margin_bottom='30px' icon_select='yes' custom_icon_color='' icon='ue808' font='entypo-fontello' av_uid='av-jl0ywt24' custom_class='' admin_preview_bg=''] [av_textblock size='' font_color='' color='' id='' custom_class='text-justify' av-medium-font-size='' av-small-font-size='' av-mini-font-size='' av_uid='av-vd96' admin_preview_bg=''] These 2 islands offer an amazing contrast despite being a short sail apart. Antigua boasts rich history and culture whilst Barbuda is the picture perfect desert island. This trip covers 5 outstanding kite spots on both islands and offers outstanding flat water conditions. <a href="https://caribbeankitecruise.com/antigua-barbuda/">Explore Antigua & Barbuda</a>. [/av_textblock]
Becomes this:
Antigua & Barbuda</a> [/av_image][av_hr class='invisible' height='5' shadow='no-shadow' position='center' custom_border='av-border-thin' custom_width='50px' custom_border_color='' custom_margin_top='30px' custom_margin_bottom='30px' icon_select='yes' custom_icon_color='' icon='ue808' font='entypo-fontello' av_uid='av-jl0ywt24' custom_class='' admin_preview_bg=''][av_textblock size='' font_color='' color='' av-medium-font-size='' av-small-font-size='' av-mini-font-size='' av_uid='av-vd96' custom_class='text-justify' admin_preview_bg=''] Ces deux îles sont étonnamment différentes bien que très proches l'une de l'autre. Antigua arbore une histoire et une culture très riche, tandis que Barbuda est à l'image de la parfaite île déserte. Au cours de cette croisière vous aurez l'opportunité de découvrir cinq superbes spots de kitesurf et de naviguer sur un plan d’eau plat translucide. Découvrez Antigua & Barbuda. [/av_textblock]
However the normal default template doesn’t parse line breaks in the shortcode as line breaks in the output HTML, so why is my French template doing this, do you think? Have I used the wrong enfold template to base my French template on? I used template-builder.php and made no other changes other than calling a different header and footer file. And I’ve tested removing those modified calls (so an identical copy of template-builder.php), and it doesn’t fix this issue.
Or is there something else that could be causing this issue that I am missing?
Any help with this would be greatly appreciated, because if I can’t solve this issue, I would have to go back and edit 40 French pages to remove the line breaks from the shortcode editor – and not all template builder elements are affected like this, so I’d first have keep figuring out which line breaks to remove.
Thank you.
Brendon
October 14, 2019 at 1:09 am #1147651Hi,
Sorry for the late reply and thanks for the great tutorial, I’m sure this will help many.
I followed your tutorial and created a template-french.php & header-french.php in my child theme, and created a page using many different elements and a french menu, not really french just in name, but I didn’t find the same error.
Even though I used text blocks and my shortcode has line spaces, I only have the normal padding and margins expected with the elements.
Here’s a screenshot of the page and the page shortcode is in the Private Content area for you to test.
If you send me a copy of your shortcode in a text file I’ll test it on my site. Does the error always occur at the very top under the header before the first element? Your screenshot looks like its occurring under an image or slider so this seems to be after the first element of the page which doesn’t seem to follow the idea that the error is with the join of the header and template?Best regards,
MikeOctober 14, 2019 at 2:46 am #1147657Hi Mike, thank you very much for taking the time to go through my tutorial and build all this to help me troubleshoot this issue. You have no idea how much I appreciate it, as I’ve tried everything I can think of to fix this without any luck.
The issue appears throughout the length of all my French pages after certain elements like between heading elements and text boxes and text boxes and buttons. It also appears between images and text boxes, but I couldn’t verify if it was happening in your test page, as I don’t have the image.
All my pages are very long and complicated and probably won’t make much sense to you since all the images will be missing etc. So to try and simplify things, I’ve added a few elements to your test shortcode where I can see the additional line break being added to the HTML source code.
I’ve attached a text file with the modified shortcode in it in the private content area. You should now see 3 columns with an h2 heading element, text box and button… if you look at the output HTML source code, you should see a <b> tag below the heading and before the text block code, and then again after the text block and before the buttons.
I’ve also verified that if I switch the page back to the default template, the line breaks are removed, and they’re added back again when I switch back to the French template. Please try this on your side to see if you can see the difference as well.
If you still need me to send you my original shortcode, I can do this as well.
Once again, thank you for your time and effort helping me with this… I look forward to hearing back from you.
Brendon
October 14, 2019 at 4:12 am #1147675Hi,
Thank you for the feedback and shortcode, I did see the rouge<br>
in the html but not in the shortcode itself, and I’m not sure why my shortcode also had line breaks which didn’t result in<br>
in the html.
The only instances of this that I could find were where someone would be using shortcodes in the default WordPress editor, such as using the shortcode wand to generate and build out their page instead of using the Advanced Layout Builder to build their page, but I didn’t think you were doing that.
Anyways I have a solution for you, try this css to hide the<br>
in the content wrapper of the page but not in text block paragraphs, I tested it on the code you sent and it worked good. It should have no effect on your other pages..entry-content-wrapper br { display: none !important; }
but if you want to be really strict with it I found my template included this class in the body
page-template-template-french
yours should be the same, so then try this css:#top.page-template-template-french .entry-content-wrapper br { display: none !important; }
then please clear your browser cache and check.
Best regards,
MikeOctober 14, 2019 at 6:20 am #1147718Hi Mike, thank you for getting back to me and for your CSS suggestion. I’ve implemented it now to take care of the issue, although it did create a few issue in areas where I’d actually used line breaks as part of my formatting, but I managed to write exclusions for all those instances.
It still boggles my mind as to why these line breaks are being added in our custom templates. If you or anyone else thinks of a reason this could be happening, please do let me know, as it would be better to still have unrestricted use of the line break tag.
And no, i’m definitely not using the default editor or the shortcode wand… I’m fully in the Advanced Layout Builder mode always. Unless Enfold’s template structure is reading it as such when we duplicate the template-builder template for some reason. But I’m definitely working in template builder mode directly.
For those interested, here is my CSS code that includes the various exclusions I needed for certain elements:
/* French Website - Remove Line Breaks */ #top.page-template-template-french .entry-content-wrapper br { display: none !important; } /* Override line-break removal for certain elements */ #top.page-template-template-french .av-image-caption-overlay br, #top.page-template-template-french .av-tab-section-tab-title-container br, #top.page-template-template-french .avia_textblock br { display: inline-block !important; }
Thank you again for all your assistance with this, and if you do think of anything else I could try, Please do let me know.
Thank you.
Brendon
October 14, 2019 at 6:38 am #1147721 -
AuthorPosts
- You must be logged in to reply to this topic.