Home › Forums › Pro Support › Custom Field repeater/URL output
Tagged: custom fields, output links, repeater
- This topic has 6 replies, 2 voices, and was last updated 5 years, 1 month ago by
Janek.
-
AuthorPosts
-
August 2, 2018 at 12:01 am #5535
Janek
ParticipantHey Tom,
I’ve got another PHP dilhemma using custom fields. I’m trying to use a repeater to output links that the client feeds into a custom field in the backend of the site.
The documentation seems to think its straight forward but I cant get it to work.
https://www.advancedcustomfields.com/resources/repeater/The code I have used wont clear in snippets:
add_action( 'wpsp_after_content', 'tu_add_material' ); function tu_add_material() { if( have_rows( 'related_material' ) ): ?> while( have_rows('related_material') ): the_row(); // vars $link = get_sub_field('link'); ?> if( $link ): ?> <a class="related-material" href="<?php echo $link['link']; ?>" target="<?php echo $link['target']; ?>"> <?php echo $link['title']; ?> /> <?php endwhile; <?php endif; }
I’m sure this is a complete abomination of PHP and I’m wondering if you could lend a hand.
Ideally what this should do is take URLS that the client inputs and then output them as clickable buttons on the front end.
I’m borrowing some code from their documentation on link custom fields as well.
https://www.advancedcustomfields.com/resources/link/But simply cant get anything to work correctly. :'(
No hurry on this one, just working on improvements on my staging site so take your time. 🙂
Happy to give you access to the back end of the site, you still have a admin account active there from last time.
http://staging.janek-dev.flywheelsites.com/Thanks again for your help and all your previous help as well. I would be completely screwed without you! 🙁
August 2, 2018 at 5:07 pm #5546Tom
KeymasterHi there,
Give this a shot instead:
add_action( 'wpsp_after_content', 'tu_add_material' ); function tu_add_material() { if ( have_rows( 'related_material' ) ) : while( have_rows('related_material') ): the_row(); $link = get_sub_field('link'); if ( $link ) : ?> <a class="related-material" href="<?php echo $link['link']; ?>" target="<?php echo $link['target']; ?>"> <?php echo $link['title']; ?> </a> <?php endif; endwhile; endif; }
August 2, 2018 at 6:01 pm #5552Janek
ParticipantInterestingly it doesn’t output anything on the front end. I looked over the code and nothing seems to be out of order. Any thoughts? 🙁
August 3, 2018 at 4:16 pm #5556Tom
KeymasterI assume
link
is the ID of your sub field? And that it has its own sub fields namedlink
,target
andtitle
?August 6, 2018 at 11:06 pm #5574Janek
ParticipantSorry for the late reply.
Yes I believe these are the correct ID’s. I’ve just doubled checked them and there is a sub field called link, however the target and title is provided by setting the field type to Link. The input on a post using the custom field would show WordPresses’s standard hyperlink window which in turn would provide data for the link, target and title fields.
I took a screenshot of the custom field post input options here:
https://drive.google.com/open?id=1SAuM1qX5XY-pw0DggNS7Ay27sCsVkMpnAs well as a screenshot of the custom field setup in the ACF plugin here:
https://drive.google.com/open?id=1QT_jpgeBvQIrTaPgBMC7BqfF8zlSzcYtPerhaps that provides a better idea than my description?
If nothing jumps out at you, it might be time to try a different approach rather than using the repeater field I’ll try to get this working with a single entry.
August 10, 2018 at 7:06 pm #5579Tom
KeymasterI’m afraid nothing is immediately jumping out at me.
Have you tried the ACF forums?
August 10, 2018 at 7:28 pm #5581Janek
ParticipantI’ve had a browse, but didnt find anything. I’ll take a better look and maybe post up something there.
Thanks for your help as always, its appreciated! 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.