0

I have used the Advanced Custom Fields plugin two create two fields, review score and review summary.

I can display these fields on a post no problem, but it displays after everything else like share buttons, author box, pagination, etc.

How can I modify my code so that the custom fields show above everything else, just after the last paragraph of the post?

This is my code to display my custom fields:

<?php if( get_field('review_score') ): ?>
                                <div class="review-score">
                                    <h1>The Last Word</h1>
                                    <div class="score-section">
                                    <span class="review-the-score"><?php the_field('review_score'); ?></span><span class="review-out-of">/10</span>
                                    </div>
                                    <div class="summary-section">
                                    <span class="review-summary"><?php the_field('review_summary'); ?></span></div>
                                </div>
                            <?php endif; ?>

Thanks for your help/
1

Plugins typically insert their content via the_content filter. The filter runs when the_content() function is called, and passes the content through the filter before output.

Remember, with a filter, you need to append to the existing content, then return the results.

add_filter( 'the_content', 'wpd_content_filter', 10 );

function wpd_content_filter( $content ){
    if ( is_single() && get_field( 'review_score' ) )
        $content .= '<span class="review-the-score">' . get_field( 'review_score' ) . '</span>';

    return $content;
}

The third argument of add_filter is the priority (10 in the above example, the default). You may need to make this a lower number if plugin content is appearing above your own.

0

I eventually got this to wrok, I decided to ditch ACF and just work with wordpress custom fields instead, here is my code which now shows the review score and summary above all other plugin stuff, author box etc.

    //show review score above other plugin stuff
add_filter( 'the_content', 'gwad_review_score', 1);

function gwad_review_score( $content ){

    global $post;

    $revscore = get_post_meta($post->ID, 'review_score', true);
    $revsummary = get_post_meta($post->ID, 'review_summary', true);

    if ($revscore) {

        $content = $content . '<div class="review-score">' . $revscore . '<p>' . $revsummary . '</p>' . '</div>';

        return $content;

    }else{

    }

}

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.