php - combine sprintf with IF condition -
i'm stuck stupid problem. asked arrange changes in php website template. here code header/hyperlink:
<?php the_title( sprintf( '<h1 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h1>' ); ?>
and should combined with:
<?php if( in_category('local-msr') ) { echo 'target="_blank" '; } ?>
i have tried this:
<?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>' ); ?>
but without succes.
any ideas on how make work? thanks!
i add third parameter sprintf
.
you on 1 line, clarity write out:
// set variable has target or empty if condition 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 variable here // ^^^^^^^ value
Comments
Post a Comment