如何设置逻辑来检查 WordPress 页面是西班牙语还是英语,然后根据不同的情况显示不同的 php 文件或字段?

问题描述 投票:0回答:1

我有一个客户在 WordPress 中拥有英语和西班牙语页面。我看到他们有一个“语言”选项卡,他们在其中设置不同的页面,但它不是 1:1 英语和西班牙语。大约有 50 个西班牙语页面和数百个英语页面。

西班牙语页面使用 /es URL 结尾。

我想做的是将逻辑添加到页脚和侧边栏 php 文件中,以检查语言是否是西班牙语,如果是,则加载一组不同的西班牙语高级自定义字段(而不是使用普通字段)英语)。或者,也许我可以为每个文件加载不同的 php 文件,但我认为 ACF 交换路线更容易或更直接。另外,它还为更多的人提供了编辑权限来更改文本字段

谁能帮我设置一下吗?我做了一些研究,但还没有找到解决方案。我假设有一个循环来检查页面语言,如果英语使用这些 ACF 字段,如果西班牙语使用其他字段,但不是 100% 如何设置。

提前致谢。我看到有关确定/回显当前语言的代码,但没有看到如何检查它,然后根据该结果有条件地显示内容

使用的语言插件是“Polylang”。

我添加了 sidebar.php 文件以供参考:

 <?php

/**
 * Sidebar for 2-column templates.
 */

?>

<div class="post-sidebar content-area">

    <?php if (get_field('sidebar_call_us_description')) : ?>
        <div class="sidebar-section">
            <img src="<?php echo get_template_directory_uri(); ?>/img/icon-contact-us.png" alt="Image of a phone headset.">
            <h1 class="sidebar-title"><?php pll_e('Call Us'); ?></h1>
            <p><?php the_field('sidebar_call_us_description'); ?></p>
            <?php if (get_field('sidebar_call_other_description')): ?>
                <h1 class="sidebar-title"><?php pll_e('Call Other'); ?></h1>
                <p><?php the_field('sidebar_call_other_description'); ?></p>
            <?php endif; ?>
        </div>
    <?php endif; ?>

    <?php $featured_post = get_field('sidebar_featured_post'); ?>
    <?php if ($featured_post): ?>
        <div class="sidebar-section featured">
            <h2 class="sidebar-title featured"><?php pll_e('Featured'); ?></h2>
            <div class="featured-post last">
                <h3 class="fpost-title"><?php echo get_the_title($featured_post->ID); ?></h3>
                <div class="fpost-image"><?php echo get_the_post_thumbnail($featured_post->ID, 'medium'); ?></div>
                <p class="fpost-excerpt"><?php the_field('page_header_excerpt', $featured_post->ID); ?></p>
                <a href="<?php echo get_permalink($featured_post->ID); ?>">Read more &rarr;</a>
            </div><!-- .featured-post -->
        </div><!-- .sidebar-section -->
    <?php endif; ?>

    <div class="sidebar-section">
        <h2 class="sidebar-title share">Share</h2>
  
        <div class="addthis_sharing_toolbox" style="display: flex;
    justify-content: center;
    gap: 5px;">
                <a href="https://www.linkedin.com/company/safe-horizon/"><img src="http://www.clientname.org/wp-content/uploads/2023/10/icon-social-linkedin.webp"></a> 
                                    <a href="https://www.facebook.com/clientname"><img src="http://www.clientname.org/wp-content/uploads/2023/10/icon-social-facebook.webp"></a>
                                        <a href="https://x.com/"><img src="http://clientname.webp" style="max-width:32px;"></a>
        </div>
    </div><!-- .sidebar-section -->

</div>
php wordpress
1个回答
0
投票

如果您使用 Polylang,您可以使用 pll_current_language 获取 2 个字母的语言代码,这样您就可以基于该代码触发逻辑。

// Use default language as a backup
$code = pll_current_language() ?? pll_default_language(); 
if ( $code == "es" ) {
  // Do one thing
} 
elseif ($code == "en") {
  // Do a different thing
}
© www.soinside.com 2019 - 2024. All rights reserved.