[英]Wordpress check if single_cat_title() contains keyword
如果类别标题包含关键字,我想有条件地将关键字添加到标题的末尾。
我正在使用以下代码,它正在打印Amish Cookies
,但我想要Amish Cookies Recipes
。
如何有条件地在标题末尾添加“食谱”?
else if( is_archive() ){
//echo __('Archive for ', 'recipe') . single_month_title(' ', false);
echo single_cat_title('Amish ') . single_month_title(' ', false) . (' Recipes');
}
您的问题尚不清楚,但是我认为这是您要的:
<?php
if( is_archive() ){
$current_category = single_cat_title("", false); // Get name of current category
// If current category contains the string "Recipes" don't add it at the end of the name
if (strpos($current_category,'Recipes') !== false) {
echo single_cat_title('Amish '). single_month_title(' ',false);
} else {
echo single_cat_title('Amish ') . single_month_title(' ',false) . (' Recipes');
}
}
?>
由于您使用Amish
对前缀进行硬编码并且不会中断您对单词Recipes
的搜索,因此您可以编写 D.R.Y。 通过仅调用 helper function single_cat_title()
一次来编写代码。
然后您可以根据您的 PHP 版本使用strpos()
或str_contains()
搜索Recipes
。
$catTitle = single_cat_title('Amish ', false);
echo $catTitle
. single_month_title(' ', false)
. (str_contains($catTitle, 'Recipes') ? ' Recipes' : '');
请注意,如果没有将第二个参数设置为false
,辅助函数将打印其有效负载而不是返回它。 将echo
与已经在打印其数据的 function 结合使用是没有意义的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.