Appending code to the_content

I’m trying to append some php code to the_content in a custom loop. I do not want to add a function because I only want to append the code to one instance of the_content, not throughout the whole site.

I’m using the Read More Right Here plugin to enable viewers to read a whole post (after clicking the more tag) that displays on the page (without going to single post). And I’m using the Advanced Custom Fields plugin to output some content (when a check box is ticked in the back end) that I want to display at the end of each post. If I place my code to output the check box data AFTER the_content in my template, the info is displayed underneath the more tag. My aim is to append this to the bottom of the_content so that it is hidden (along with the rest of the post after the more tag) until you click ‘read more’ (and the rest of the post is revealed).

Read More

The code I am trying to append to the end of the_content is:

<?php
if( in_array( 'frazer-barton', get_field('who_specialises') ) )
{
echo '<img src="http://andersonlloyd.php.wired.co.nz/wp-content/themes/all/images/example-thumb.jpg" />';
}
if( in_array( 'sarah-simmers', get_field('who_specialises') ) )
{
echo '<img src="http://andersonlloyd.php.wired.co.nz/wp-content/themes/all/image/example2-thumb.jpg" />';
}
else {
echo '';
}
?>

I am not a PHP developer so any help on this is hugely appreciated.

Related posts

Leave a Reply

1 comment

  1. You are looking to filter the_content.

    In your theme’s functions.php or in a plugin:

    function wpse74288_content_filter( $content ) {
        /* save field into variable, such as to call get_field only once
         * not required, but more efficient */
        $who_specialises = get_field('who_specialises');
        /* append to content on condition */
        if ( in_array( 'frazer-barton', $who_specialises ) ) {
            /* use correct img src attributes, shortened only for representation here */
            $content .= '<img src="http://andersonlloyd...images/example-thumb.jpg" />';
        } elseif ( in_array( 'sarah-simmers', $who_specialises ) ) {
            $content .= '<img src="http://andersonlloyd.../images/example2-thumb.jpg" />';
        }
        /* return the content, whether appending happened or not */
        return $content;
    }
    add_filter( 'the_content', 'wpse74288_content_filter' );
    

    As an aside, this question is probably the dupe of a dupe of a dupe [+ n dupes], had you known what to search for.