wordpress - 在 WordPress 函数中使用 if 模板等于
问题描述
我正在使用这个功能:
if ( $id && $post && $post->post_type == 'business-areas' )
{
$pieces = explode(' ', $the_title);
$last_word = trim(strtolower(array_pop($pieces)));
if($last_word != 'jobs')
{
$the_title = $the_title . ' jobs';
}
}
return $the_title;
如果标题的最后一个单词不是 JOBS,这基本上是将单词 JOBS 附加到业务领域的自定义帖子类型的任何标题上。
有没有办法为此添加模板查询?
所以基本上,如果自定义帖子的页面模板是DEFAULT,就这样做,如果页面模板不是DEFAULT,就不要这样做。
解决方案
将此代码放入functions.php
文件中。
function add_label_to_post_title( $the_title = '' ) {
global $post;
if ($post && $post->post_type == 'business-areas' )
{
$pieces = explode(' ', $the_title);
$last_word = trim(strtolower(array_pop($pieces)));
if($last_word != 'jobs')
{
$the_title = $the_title . ' jobs';
}
}
return $the_title;
}
add_filter( 'the_title', 'add_label_to_post_title' );
推荐阅读
- reactjs - 当状态是数据库表时,如何从子级更改父级的状态?
- python - Django 用户表单未显示在管理页面中
- javascript - React Datepicker 包括没有周末的未来几天
- r - 如何编写R函数
- sitefinity - sitefinity ActionResult 缺少查询字符串
- xml - XSLT:如何调用以字符串形式给出的用户定义函数
- python - 如何在 Jupyter 笔记本中使用 Pandas 永久更改数据框样式?
- python - 具有多个键的 Python 字典列表
- sql - 在 SQL Server 中选择以显示表 OnHand、Sale 和 Purchase 中的所有不同行,其中一个/或 Qty 字段不为空
- c++ - ffmpeg/Libavcodec 找不到编解码器