0

I am trying to add custom fields at the end of posts.

I succeeded showing the custom content using the below mentioned code. But the main post's content was replaced. :

function custom_content_filter_the_content( ) {
   the_field("field1");
}
add_filter( 'the_content', 'custom_content_filter_the_content' );

So I want to try this as in below mentioned example:

function custom_content_filter_the_content( $content ) {
    $custom_content = 'some_content_appears_here';
    $custom_content .= $content;
    return $custom_content;
}
add_filter( 'the_content', 'custom_content_filter_the_content' );

Want to replace 'some_content_appears_here' with <span class="custom_fds"><?php if( function_exists('the_field') ) the_field('field1'); ?></span>.

Could any tell me how to use the <?php the_field() ?> with in functions.php to add custom fields(created with Advanced custom fields) at the end of the post content(that is easy when we use in templates but not in funcitons.php) ?

Update: @G.M 's code helps me to achieve what I want.

Here is the final code:

function custom_content_filter_the_content( $content ) {
    if ( function_exists('get_field') ) {
      $content .= '<span class="custom_fds">' . get_field('field1') . '</span>';
      $content .= '<span class="custom_fds">' . get_field('field2') . '</span>';          
    }
    return $content;
}
add_filter( 'the_content', 'custom_content_filter_the_content' );

Thanks to @G.M. I have a few questions related to the custom content.

  • I am displaying multiple fields in the single function. Is that ok?
  • Instead of the end or beginning of the post, is there a way to choose a position for custom content any place with in the_content ? For example, if I use any plugins for social sharing, they display the content at the same place, I want to change positions of these two types of custom content.
  • 1. How many fields you want to show is completely upto you or your requirement. So you are the best person to judge this. 2. Yes, generally you can place it at a custom position by using shortcodes. Please check the plugin documentation or support team to confirm if they provide any such shortcode or not. – Chittaranjan Dec 15 '13 at 8:56
2

You can simply use get_field that returns the field instead of the_field that echo it.

function custom_content_filter_the_content( $content ) {
    if ( function_exists('get_field') ) {
      $content .= '<span class="custom_fds">' . get_field('field1') . '</span>';
    }
    return $content;
}
add_filter( 'the_content', 'custom_content_filter_the_content' );
  • As per @IamSJ, he has mentioned posts. But your code will add the content for pages as well. So shouldn't there be a condition check for post type? – Chittaranjan Dec 15 '13 at 8:45
  • Right, this function will add the custom field to a lot more than just post-content. It's also added to the excerpt, which often is displayed in the <head> as meta-description – Philipp May 30 '16 at 23:17

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.