我被一个愚蠢的问题困住了。我被要求在 php 网站模板中安排一些更改。所以这里是标题/超链接的代码:
<?php the_title( sprintf( '<h1 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h1>' ); ?>
Run Code Online (Sandbox Code Playgroud)
这应该与:
<?php if( in_category('local-msr') ) { echo 'target="_blank" '; } ?>
Run Code Online (Sandbox Code Playgroud)
我试过这样的事情:
<?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>' ); ?>
Run Code Online (Sandbox Code Playgroud)
但没有成功。
关于如何使这项工作的任何想法?谢谢!
我会添加第三个参数到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
Run Code Online (Sandbox Code Playgroud)