Creating a plugin to show Advanced Custom Fields data (code mostly written)
-
Hi all!
Site I’m working on is a radio show. I have a bunch of custom fields, courtesy of Advanced Custom Fields, for links to musicians’ websites, social profiles, etc.. For the last few years, we’ve used a Genesis child theme to display them, and the code I adapted worked great. The radio show’s host/producer now wants to move to a non-Genesis theme, and I’m having some trouble making it work.
Here’s an example from the site we’re migrating FROM, which includes the functioning code. (The Artist Links box, floated to the right of the main copy.)
Here’s the same post at the new site, where the code doesn’t yet display. (It will display in the same place.) FWIW, the theme is GrandBlog.
Here’s the code:
<?php add_action ('if_artist_links'); function if_artist_links() { // These are the custom field keys defined in the dashboard: $metas_links = array( 'website', 'album', 'tour', 'twitter', 'facebook', 'bandcamp', 'soundcloud', 'youtube', 'instagram', 'linkedin', 'myspace', 'image', ); // If _any_ ACF field is filled in, it's a go. $has_meta = false; // init foreach ($metas_links as $test ) { if ( get_field($test) ) { $has_meta = true; continue; // Just need one meta field filled to create the div. } } if ( $has_meta ) { echo '<div class="custom-data artist-links">'; echo '<h2 class="artistname">' ?> <?php the_field('name') ?></h2> <center><strong> <?php the_field('tribe') ?></strong></center> <?php foreach ( $metas_links as $meta_links ) { $$meta_links = get_field($meta_links); if ( $$meta_links ) { $f_object = get_field_object($meta_links); $label = $f_object['label']; echo '<div class="' . $meta_links . '">' . '<a href="'. $$meta_links .'" target="blank"><span class="label">' . $label . '</a></span>' . '</div>'; } } echo '</div><!-- /.custom-data -->'; } } ?>
Where there’s an ACF value, a link is created. Where there’s no ACF value, there’s no link. Just one link is required to make the box appear.
As far as I can tell, I’m just having a display issue. i.e., I’m not telling the code to run in the right way. If you can help me do that, I’ll sing your praises through the weekend.
I’d really like to wrap this up in a plugin, for simplicity and ease of maintenance, and to put minimal code in single.php.
Thanks so much for your help!
The page I need help with: [log in to see the link]
- The topic ‘Creating a plugin to show Advanced Custom Fields data (code mostly written)’ is closed to new replies.