3

=====THIS POST HAS BEEN UPDATED===== SCROLL TO THE BOTTOM TO READ THE UPDATE!

I asked a question yesterday that a gentleman named Tim was nice enough to take a stab at. He helped me through writing a filter that will enable me to append the_content with a custom meta box I created using Advanced Custom Fields.

Tom got me through almost the entire process, but unfortunately wasn't able to help me complete it. I am no pro programmer by any stretch, but I have (for the last few hours) tried to decipher what his code means and why it might be failing to pull in the text from the back end... And I keep coming up short.

Here is the link to my previous post : It takes you from start 'till now

and here is the code I have right now:

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 = $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 = $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 = $string_to_add . '</div>';
}
$content = $content . $string_to_add;
return $content;

}

add_filter('the_content', 'weedub_affiliate_filter', 9);

This snippet is ALMOST there, it is displaying in the right place, and it is recognizing that I have entered 3 rows of fields on the backend, but it is not printing the text from the fields. example (scroll down the page till you see where I have written END OF THE_CONTENT in heading case) : Look for the "Weedub Product Recommendations" box

Any and all help is soooooo appreciated! THANKS -Aaron

=====UPDATE====

So, I found out that the code DOES actually print the values, but it prints them BEFORE the_content, and the markup AFTER the_content!!

look just before the content actually starts (after the featured image) and you will see the string of values... then scroll down to the end of the_content and you will see the markup for the meta box, (titled "weedub product recommendations") here is the link again: link to single post

I can't figure out where I am going wrong here!!

1
  • 1
    Hi Klyde, and welcome to WPSE! If you've found an answer to your question, you should post it as an answer (yes, you're allowed to answer your own question). Then you can mark your answer as the correct answer, and the system will mark your question as solved - no need for editing of title on your part. And that way you can get upvotes both for your question and for your answer :)
    – Lea Cohen
    Apr 2, 2012 at 5:44

1 Answer 1

2

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.

2
  • The person who helped you was TIM, not TOM :)
    – Devner
    Apr 15, 2012 at 8:23
  • edited, guess I got a little too excited to be as polite as I normally am, no disrespect meant by it.
    – Klyde
    Apr 15, 2012 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.