首页 > 解决方案 > 在 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,就不要这样做。

标签: wordpress

解决方案


将此代码放入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' );

推荐阅读