如何更改按钮文本,取决于网站 slug (PHP, Wordpress)
How can I change button text, depends on site slug (PHP, Wordpress)
首先,我想说我对PHP一无所知(这完全不是我的鞋子)。我被要求在 Wordpress 主题中制作一些东西,这将翻译按钮中的文本,具体取决于网站语言。差异仅在 slug 中可见,所以我假设,我必须以某种方式获取信息是 slug 包含 "en" 或 "de" 并将该信息传递给翻译函数。
问题是,我不知道从哪里开始。我将不胜感激任何帮助。
<div class="read-more clearfix">
<a class="button post-button" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php esc_html_e('Read more', 'astrid'); ?></a>
</div>
将此代码放入 functions.php 文件
if(strpos($_SERVER['REQUEST_URI'], 'de') !== false ){
//If your slug is de
_e('<div class="read-more clearfix">
<a class="button post-button" href="'.the_permalink().'" title="'. the_title().'">'.esc_html("Read more", "astrid").'</a>
</div>');
}
else{
//If your slug is en
_e('<div class="read-more clearfix">
<a class="button post-button" href="'.the_permalink().'" title="'.the_title().'">'.esc_html("Read more", "astrid").'</a>
</div>');
}
如果你只想替换一个按钮文本,上面的代码很好,但如果你想在所有页面上设置,请使用基于语言的翻译插件。
我认为它能胜任。
function the_title(){
$language = $_GET["slug"];
$buttonText = "";
switch($language){
case "fr":
$buttonText = "Le texte de mon button";
break
case "de":
$buttonText = "Mein Schaltflächentext";
break
// etc
case default:
$buttonText = "My button text";
break
}
return $buttonText;
}
但是,如果您想翻译更多内容,则需要使用基于语言翻译的插件或创建您自己的插件。因为否则它将很快变得难以维护
<?php elseif(strpos($_SERVER['REQUEST_URI'], 'en') !== false ): ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div>
<div class="read-more clearfix">
<a class="button post-button" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php esc_html_e('Read more', 'astrid'); ?></a>
</div>
<?php else : ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div>
<div class="read-more clearfix">
<a class="button post-button" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php esc_html_e('Czytaj więcej', 'astrid'); ?></a>
</div>
<?php endif; ?>
首先,我想说我对PHP一无所知(这完全不是我的鞋子)。我被要求在 Wordpress 主题中制作一些东西,这将翻译按钮中的文本,具体取决于网站语言。差异仅在 slug 中可见,所以我假设,我必须以某种方式获取信息是 slug 包含 "en" 或 "de" 并将该信息传递给翻译函数。
问题是,我不知道从哪里开始。我将不胜感激任何帮助。
<div class="read-more clearfix">
<a class="button post-button" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php esc_html_e('Read more', 'astrid'); ?></a>
</div>
将此代码放入 functions.php 文件
if(strpos($_SERVER['REQUEST_URI'], 'de') !== false ){
//If your slug is de
_e('<div class="read-more clearfix">
<a class="button post-button" href="'.the_permalink().'" title="'. the_title().'">'.esc_html("Read more", "astrid").'</a>
</div>');
}
else{
//If your slug is en
_e('<div class="read-more clearfix">
<a class="button post-button" href="'.the_permalink().'" title="'.the_title().'">'.esc_html("Read more", "astrid").'</a>
</div>');
}
如果你只想替换一个按钮文本,上面的代码很好,但如果你想在所有页面上设置,请使用基于语言的翻译插件。
我认为它能胜任。
function the_title(){
$language = $_GET["slug"];
$buttonText = "";
switch($language){
case "fr":
$buttonText = "Le texte de mon button";
break
case "de":
$buttonText = "Mein Schaltflächentext";
break
// etc
case default:
$buttonText = "My button text";
break
}
return $buttonText;
}
但是,如果您想翻译更多内容,则需要使用基于语言翻译的插件或创建您自己的插件。因为否则它将很快变得难以维护
<?php elseif(strpos($_SERVER['REQUEST_URI'], 'en') !== false ): ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div>
<div class="read-more clearfix">
<a class="button post-button" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php esc_html_e('Read more', 'astrid'); ?></a>
</div>
<?php else : ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div>
<div class="read-more clearfix">
<a class="button post-button" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php esc_html_e('Czytaj więcej', 'astrid'); ?></a>
</div>
<?php endif; ?>