Hi,
I’m putting together a list using a repeater field. Some of the list items will be links and others will just be plain text. I want to make this as easy as possible for the user, so I set up a repeater field with a “text” and a “url” item. On the edit page, it shows up like so:
1. [TEXTAREA] | [URL]
2. [TEXTAREA] | [URL]
3. [TEXTAREA] | [URL]etc…
I currently have the list item set up as
<li><a href="<?php the_sub_field('item_url'); ?>" target="_blank"><?php // display a sub field value
the_sub_field('list_item'); ?></a></li>
This works, except for when a url is not typed… The text still shows, but it shows as a clickable link and I don’t want this. I have tried experimenting with “if” statements to try and stop this (eg. if the link is not empty, show above, otherwise just show plain text) but I can’t seem to code it properly without it completely tearing apart the whole page….
Can anyone help with this please?
Thanks!
get the url and see if it has a value
<li>
<?php
$text = get_sub_field('list_item');
$url = get_subfield('item_url');
if ($url) {
$text = '<a href="'.$url.'" target="_blank">'.$text.'</a>';
}
echo $text;
?>
</li>