微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 将sprintf与IF条件相结合

我遇到了一个愚蠢的问题.有人要求我在PHP网站模板中安排一些更改.所以这是标题/超链接代码

<?PHP the_title( sprintf( '<h1 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h1>' ); ?>

这应该结合:

<?PHP if( in_category('local-msr') ) { echo 'target="_blank" '; } ?>

我尝试过这样的事情:

<?PHP the_title( sprintf( '<h1 class="entry-title"><a href="%s" rel="bookmark" if( in_category('local-msr') ), echo 'target="_blank">', esc_url( get_permalink() ) ), '</a></h1>' ); ?>

但没有成功.

关于如何使这项工作的任何想法?谢谢!

解决方法:

我会在sprintf中添加第三个参数.

你可以在一条线上完成所有工作,但为了清楚起见,我会把它写出来:

// set a variable that has the target or is empty if the condition is not met
$target = in_category('local-msr') ? 'target="_blank"' : '';
the_title( sprintf( '<h1 class="entry-title"><a href="%s" %s rel="bookmark">', esc_url( get_permalink() ), $target ), '</a></h1>' );
//                                                        ^^ add the variable here
//                                                                                                         ^^^^^^^ the value

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐