{"id":3124,"date":"2021-07-18T14:59:21","date_gmt":"2021-07-18T06:59:21","guid":{"rendered":"https:\/\/oangle.com\/?p=3124"},"modified":"2021-07-18T15:10:46","modified_gmt":"2021-07-18T07:10:46","slug":"wordpress-how-to-optimise-acf-link-field-types-by-using-link-array-as-a-return-value","status":"publish","type":"post","link":"https:\/\/oanglev7.oanglelab.com\/wp\/wordpress-how-to-optimise-acf-link-field-types-by-using-link-array-as-a-return-value\/","title":{"rendered":"WordPress &#8211; How To Optimise ACF Link Field Types By Using Link Array As A Return Value"},"content":{"rendered":"\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/www.advancedcustomfields.com\/\" target=\"_blank\">Advanced Custom Field (ACF)<\/a> is a great plugin that helps ease the gap between PHP coding in WordPress \u2014 from the WP-admin fields and the saving to database. At Oangle, we use the Pro version of ACF plugin for 90% of our projects. There is also a free version of it that is a great tool for any aspiring WordPress developer.<\/p>\n\n\n\n<p>In this article, we&#8217;d like to share a handy piece of code for the link field of ACF. In the field type options, you&#8217;ll be able to select &#8216;Link&#8217;. This allows you to use the default WordPress link feature, which helps you to save 2 fields when creating your ACF setup.<\/p>\n\n\n\n<p>For example, without using the link field, you will need the following 3 fields to set up a link:<br>(i) Text field &#8211; for the link\/button text<br>(ii) Link URL &#8211; for the link&#8217;s url<br>(iii) Target state &#8211; for users to select whether they want the link to open up in a new tab or not.<\/p>\n\n\n\n<p>Using the link field, however, admin users will see this when trying to add the link:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/oanglev7.oanglelab.com\/wp-content\/uploads\/2021\/07\/oangle_blog_acf_link_select_link.png\" alt=\"\" class=\"wp-image-3126\"\/><\/figure>\n\n\n\n<p>On clicking it, they will see the default WordPress link feature:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"506\" height=\"233\" src=\"https:\/\/oanglev7.oanglelab.com\/wp-content\/uploads\/2021\/07\/oangle_blog_acf_link_window.png\" alt=\"insert\/edit link\" class=\"wp-image-3127\"\/><\/figure>\n\n\n\n<p>This combines the 3 required field into one \u2014 a very admin friendly field for call-to-actions (CTAs), buttons and links.<\/p>\n\n\n\n<section class=\"single-blog-block view-our\">\n    <div class=\"__container\">\n                <h2 class=\"text-center\">How to add active state to your custom post-type link in a menu<\/h2>\n                            <\/div>\n<\/section>\n\n\n\n<p>However, there is an important setting for the field type, known as &#8216;return value&#8217;:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"526\" height=\"412\" src=\"https:\/\/oanglev7.oanglelab.com\/wp-content\/uploads\/2021\/07\/oangle_blog_acf_link.png\" alt=\"\" class=\"wp-image-3128\"\/><\/figure>\n\n\n\n<p>As lay developers, it is always easier to set the return value to &#8216;link url&#8217;, because you&#8217;ll be able to easily use the field in your PHP template file by just:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php the_field('field_name'); ?><\/code><\/pre>\n\n\n\n<p>However, in that case, you will not be able to utilise the &#8216;link text&#8217; field and the target state setting.<\/p>\n\n\n\n<p>Upon setting your return value to &#8216;link array&#8217;, use the below piece of code to display links in your PHP template file:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/zecqi\/eaedbd36dd71999d60363d384ebc1582.js\"><\/script>\n\n\n\n<p>Remember to change the name of your field in the above code.<\/p>\n\n\n\n<p>Unsure of where to insert this? Don\u2019t want to break anything? <a href=\"https:\/\/oanglev7.oanglelab.com\/about\/\" target=\"_blank\" rel=\"noreferrer noopener\">Our team<\/a> of WordPress developers in Singapore can accomplish this job for you safely and efficiently. We are highly experienced in <a href=\"https:\/\/oanglev7.oanglelab.com\/services\/wordpress-development\" target=\"_blank\" rel=\"noreferrer noopener\">WordPress<\/a> and <a href=\"https:\/\/oanglev7.oanglelab.com\/services\/web-development\" target=\"_blank\" rel=\"noreferrer noopener\">web development<\/a> and are equipped in solving any issues you might be facing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we&#8217;d like to share a handy piece of code for the link field of ACF. This helps you to save 2 fields when creating your ACF setup on WordPress.<\/p>\n","protected":false},"author":2,"featured_media":3134,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_seopress_robots_primary_cat":"none","_seopress_titles_title":"How To Optimise ACF Link Field Types On WordPress | Oangle","_seopress_titles_desc":"In this article, we share a handy piece of code for ACF link fields. This lets you use the WordPress link feature, which helps when creating ACF setups.","_seopress_robots_index":"","footnotes":""},"categories":[21],"tags":[],"class_list":["post-3124","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress-tips"],"acf":[],"_links":{"self":[{"href":"https:\/\/oanglev7.oanglelab.com\/wp\/wp-json\/wp\/v2\/posts\/3124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oanglev7.oanglelab.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oanglev7.oanglelab.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oanglev7.oanglelab.com\/wp\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/oanglev7.oanglelab.com\/wp\/wp-json\/wp\/v2\/comments?post=3124"}],"version-history":[{"count":10,"href":"https:\/\/oanglev7.oanglelab.com\/wp\/wp-json\/wp\/v2\/posts\/3124\/revisions"}],"predecessor-version":[{"id":3139,"href":"https:\/\/oanglev7.oanglelab.com\/wp\/wp-json\/wp\/v2\/posts\/3124\/revisions\/3139"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/oanglev7.oanglelab.com\/wp\/wp-json\/wp\/v2\/media\/3134"}],"wp:attachment":[{"href":"https:\/\/oanglev7.oanglelab.com\/wp\/wp-json\/wp\/v2\/media?parent=3124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oanglev7.oanglelab.com\/wp\/wp-json\/wp\/v2\/categories?post=3124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oanglev7.oanglelab.com\/wp\/wp-json\/wp\/v2\/tags?post=3124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}