Support » Fixing WordPress » Creating a plugin to show Advanced Custom Fields data (code mostly written)

  • Resolved Adam Rubinstein

    (@gourmetbooks)



    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]

Viewing 2 replies - 1 through 2 (of 2 total)
  • As I continue to play with it, I think I can clarify what I need help with. I’m specifically struggling with add_action and do_action. I’m pretty sure my add_action should go in the functions.php (or a plugin file), and the do_action in the single.php. I’m getting the following errors pretty frequently:

    call_user_func_array() expects parameter 1 to be a valid callback, no array or string given

    and

    Missing argument 2 for add_action(),

    Fixed. I posted the same question on Stack Overflow. For anyone who’s having the same or similar trouble, here was the solution:

    function single_add_to_content( $content ) {    
        if( is_single() ) {        
            // 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 ) {
                $content.= '<div class="custom-data artist-links">';
                $content.= '<h2 class="artistname">'.get_field('name').'</h2>
                <center><strong>'.get_field('tribe').'</strong></center>';
                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'];
                        $content.= '<div class="' . $meta_links . '">' .
                        '<a href="'. $$meta_links .'" target="blank"><span class="label">' . $label . '</a></span>' . 
                        '</div>';
                    }
                }
                $content.= '</div><!-- /.custom-data -->';
            }
        }
        return $content;
    }
    add_filter( 'the_content', 'single_add_to_content' );
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.