Support

Account

Home Forums Add-ons Repeater Field URL field help

Solving

URL field help

  • I want to create a repeater field that displays the TEXT on my website but opens up the URL when the TEXT is clicked on.

    URL: https://www.test-website.com
    TEXT: Test Site

    similar to the “Places To Visit While Staying At Swallowdale On The Norfolk Broads” list at the bottom of this page.
    https://www.swallowdale-brundall.co.uk/norfolk-broads/

    Is this possible please?
    Thanks

  • Sure, I do this every day – use the “Link” element. It gives you the URL and the Title:

    https://www.advancedcustomfields.com/resources/link/

    <?php 
    $link = get_field('link');
    if( $link ): 
        $link_url = $link['url'];
        $link_title = $link['title'];
        $link_target = $link['target'] ? $link['target'] : '_self';
        ?>
        <a class="button" href="<?php echo esc_url( $link_url ); ?>" target="<?php echo esc_attr( $link_target ); ?>"><?php echo esc_html( $link_title ); ?></a>
    <?php endif; ?>
  • Thanks, is there an example of how to use this with a repeater field please?

  • <?php if ( have_rows( 'my_repeater' ) ) : ?>
    	<?php while ( have_rows( 'my_repeater' ) ) : the_row(); ?>
    		<?php $url = get_sub_field( 'url' ); ?>
    		<?php if ( $url ) { ?>
    			<a href="<?php echo $url['url']; ?>" target="<?php echo $url['target']; ?>"><?php echo $url['title']; ?></a>
    		<?php } ?>
    	<?php endwhile; ?>
    <?php else : ?>
    	<?php // no rows found ?>
    <?php endif; ?>

    The same, just use “get_sub_field”.

    If you struggle with the code there is a premium plugin that gives you all the code: https://hookturn.io/downloads/acf-theme-code-pro/

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.