Custom filter for the_content doesn’t work correctly

I don’t exactly know what happened, but I got it working! All cleaned up and ready to use… Here you go guys:

 /**
* 
* This is the filter that adds the affiliate box to the end of the article
* 
*/
add_filter('the_content', 'weedub_affiliate_filter', 9);
function weedub_affiliate_filter($content) 
    {
        $string_to_add = '';
        // only add on single posts with aff checkbox and label
        if (is_single() && get_field('affiliate_checkbox') && get_field('affiliate_label')) 
            {
                $string_to_add .= '<div class="weedub_meta_box"><div class="weedub_meta_title"><span>Weedub Product Recommendations</span></div>';
                while (the_repeater_field('affiliate_label')) 
                    {   
                        // list affiliates
                        $string_to_add .= '<div class="weedub_meta_item"><div class="weedub_meta_label"><span>' . get_sub_field('label_affiliate') . '</span></div><div class="weedub_meta_value"><a href="' . get_sub_field('link_affiliate') . '" target="_blank" alt="reference link" title="reference link">' . get_sub_field('text_for_link_affiliate') . '</a></div></div>';
                    }
                $string_to_add .= '</div>';
            }
        $content .= $string_to_add;

        return $content;
    }

Let me know if anyone out there can clean this up or optimize it, cause I’m sure it could be tweaked further.

Thanks to @tim for the majority of this code.

Leave a Comment