Hello friends, Can someone give me a little help? I have this code: if(get_field( "nofollow_tag" )) { $nofollow_tag = 'rel="nofollow noopener"'; } else { $nofollow_tag = ''; } PHP: And now I need to add this code to show the value: <?php echo $nofollow_tag; ?> PHP: The idea is to show in this line: Since that code is already between php tags, how can I replace the #CODEHERE# with my code? My php knowledge is very poor to do this... probably I need to create a variable right? Thanks!!
Hi, probably someting like below? <?php $output = sprintf( '%s <a class="link" %s href="%s" %s>%s %s %s %s %s %s %s</a> %s', $site_icon, $target, $href, $nofollow_tag, $title, $sticky_icon, $additional_icon, $mobile, $new_icon, $extra, $hover, $review ); ?> Ref:http://php.net/manual/en/function.sprintf.php
... and people wonder why I call sprintf halfwit garbage that's a remnant of early C compilers being useless single-pass rubbish that has ZERO place in modern code. STOP making the parser work twice as hard as it has to! <?php $output = '<a class="link" ' . $site_icon . ' href="' . $target . '"' . $nofollow_tag . '>' . $title . ' ' . $sticky_icon . ' ' . $additional_icon . ' ' . $mobile . ' ' . $new_icon . ' ' . $extra . ' ' . $hover . '</a>' . $review; ?> Code (markup): Of course if you are only using that nofollow_tag variable once, don't waste a 'variable for nothing' on it -- and even if you were, ternary operators are your friend. Makes me wonder just how many 'variables for nothing' you've got that vomiting up in there. <?php $output = '<a class="link" ' . $site_icon . ' href="' . $target . '"' . (get_field( "nofollow_tag" ) ? ' rel="nofollow noopener"' : '' ) . '>' . $title . ' ' . $sticky_icon . ' ' . $additional_icon . ' ' . $mobile . ' ' . $new_icon . ' ' . $extra . ' ' . $hover . '</a>' . $review; ?> Code (markup): Loses the variable for nothing and does the comparison right there in the $output creation. Either way, 99.99% of the time you see sprintF, somebody is horribly overthinking something simple.