0

I'm trying, but struggling to add a specific div to the top of a post, based on the category it is assigned.

For example, at the top of the content, if it's tagged with 'basketball' I want to add specific sponsors and information within a div.

Any ideas on the best/cleanest way to achieve this?

1 Answer 1

0

You can use the the_content filter for this:

add_filter( 'the_content', 'wpse420024_filter_content' );
/**
 * Conditionally adds content above the post content.
 *
 * @param  string $content The post content.
 * @return string          The filtered post content.
 */
function wpse420024_filter_content( $content ) {
    // Only run on the single post view.
    if ( is_single() ) {
        if ( has_tag( 'basketball' ) ) {
            $content = '<div>My Additional Content</div>' . $content;
        }
    }
    return $content;
}

This code is untested. Try it out before putting it into production.

References

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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