admin管理员组文章数量:1122826
Please forgive this totally newb question. I'm not a dev, and I'm a little too far that I can deal with right now...
I'd like to add a link to the WooCommerce cart page using the cart icon from my menu located at: . I have a friend who coded the php function and I believe I just have to add it in the functions.php
file of my child theme. Here is the code of the function :
// cart display function
function directByLang()
{
$current_lang = apply_filters('wpml_current_language', NULL);
if ($current_lang == 'en') {
// English link
echo '<a href="/en/cart/">English Cart</a>';
} elseif ($current_lang == 'fr') {
// French link
echo '<a href="/panier/">French Panier</a>';
} else {
// Default to French if language is not determined
echo '<a href="/panier/">French Panier</a>';
}
}
Now first of all neither my friend nor I know if this function is properly coded because my friend never worked with WP... And I'm no dev...
On my home page, there is a shopping cart icon with a red bubble with the number of items in the cart. When this is clicked, it opens a popup cart that I want to hide. I was able to hide this ugly popup with css using display: none;
But I would like that the cart icon could direct the users to either the French cart (/panier/) or the English cart (/en/cart/) onclick on the icon, depending on the language selected by the users when they entered the site.
I'm pretty sure this has something to do with css but honestly I'm overwhelmed, as I have a very steep learning curve ahead of me.
I know I'm asking a lot, and any help will be appreciated. I gotta start somewhere, if I want to learn these things one day... Thanks.
Please forgive this totally newb question. I'm not a dev, and I'm a little too far that I can deal with right now...
I'd like to add a link to the WooCommerce cart page using the cart icon from my menu located at: http://gimp4.devcaneve.ca. I have a friend who coded the php function and I believe I just have to add it in the functions.php
file of my child theme. Here is the code of the function :
// cart display function
function directByLang()
{
$current_lang = apply_filters('wpml_current_language', NULL);
if ($current_lang == 'en') {
// English link
echo '<a href="/en/cart/">English Cart</a>';
} elseif ($current_lang == 'fr') {
// French link
echo '<a href="/panier/">French Panier</a>';
} else {
// Default to French if language is not determined
echo '<a href="/panier/">French Panier</a>';
}
}
Now first of all neither my friend nor I know if this function is properly coded because my friend never worked with WP... And I'm no dev...
On my home page, there is a shopping cart icon with a red bubble with the number of items in the cart. When this is clicked, it opens a popup cart that I want to hide. I was able to hide this ugly popup with css using display: none;
But I would like that the cart icon could direct the users to either the French cart (/panier/) or the English cart (/en/cart/) onclick on the icon, depending on the language selected by the users when they entered the site.
I'm pretty sure this has something to do with css but honestly I'm overwhelmed, as I have a very steep learning curve ahead of me.
I know I'm asking a lot, and any help will be appreciated. I gotta start somewhere, if I want to learn these things one day... Thanks.
Share Improve this question edited Apr 4, 2024 at 15:53 Tony Djukic 2,2594 gold badges18 silver badges33 bronze badges asked Apr 2, 2024 at 23:13 PatrickPatrick 211 bronze badge1 Answer
Reset to default 0There's a few things to consider here.
1) You should always first check for a third-party plugin like WPML. If, for whatever reason, WPML stops working or gets deactivated, assuming it's there could result in a fatal error.
function directByLang() {
if( defined( 'ICL_SITEPRESS_VERSION' ) ) {
$current_lang = apply_filters('wpml_current_language', NULL);
if ($current_lang == 'en') {
// English link
echo '<a href="/en/cart/">English Cart</a>';
} elseif ($current_lang == 'fr') {
// French link
echo '<a href="/panier/">French Panier</a>';
} else {
// Default to French if language is not determined
echo '<a href="/panier/">French Panier</a>';
}
} else {
// Default to French if WPML not active
echo '<a href="/panier/">French Panier</a>';
}
}
I haven't used WPML in a few years so not sure if this is the correct method anymore. I found it here: https://wpml.org/forums/topic/check-if-wpml-is-active/
2) This does check for the language and then depending on the language it outputs the different cart URLs. But just adding this snippet/function alone to your functions.php
won't do anything. Because it doesn't get loaded anywhere. This is basically a template tag, so in the file where you want this to appear, you have to add <?php directByLang(); ?>
.
I would assume that you want it in your header.php
.
For example, in your header.php
you'll have some <div>
and <nav>
elements that organize he logo/site name, navigation, search field, etc...
Somewhere in there, where it makes sense, you'll want to add <?php directByLang(); ?>
so that the function executes in there.
You'll probably need some CSS, maybe even wrap it in it's own container, like so:
<div class="multi-language-cart-link">
<?php directByLang(); ?>
</div>
Hope that helps.
本文标签: Calling a PHP function from a menu item
版权声明:本文标题:Calling a PHP function from a menu item 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736311523a1934768.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论