Im using advanced custom fields and I want to display a line if both fields are active.
I currently have one checking for a spotify link and one checking for an iTunes link. What I would like to do is display a | (to separate the text) if both fields are being used otherwise the | remains hidden.
Here is my code:
<?php if( get_field('spotify') ): ?>
<a target="_blank" href="<?php the_field('spotify');?>">Spotify</a>
<?php endif; ?>
<?php if( get_field('spotify','itunes') ): ?>
|
<?php endif; ?>
<?php if( get_field('itunes') ): ?>
<a target="_blank" href="<?php the_field('itunes');?>">iTunes</a>
<?php endif; ?>
Why don’t you try :
You could also avoid the reuse of the
get_field
function :Edit : you may have a close result using just this CSS :
demo
You also could make good use of
get_fields( $postID )
function. Like so:You can actually achieve this using just CSS. What you can do is make use of the
+
selector in CSS. If there are two of them, like – let’s say<span>
‘s, then you could select the second one using the+
selector and add a pseudo:before
to it withcontent: " | ";
The best thing about this is it’s all on the client side and does not require your server to do multiple
get_field()
‘sThere’s only two that you need to pull and that’s too much for ACF a
get_fields()
.Another good thing is if you need to add more in the future, you don’t have to do another check like you are doing right now with the current solution.
See my example bellow:
I have created the following snippet in which you can just add your links in the array
$fields
, and it displays everything in the same manner as you had, but without all the weird conditionals:The advantage of this code is that it still works with more than 2 fields…