How to add content at the end of posts?
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.
You can simply use
get_field
that returns the field instead ofthe_field
that echo it.