custom field - 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 c

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.

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.
Share Improve this question edited Dec 15, 2013 at 5:21 Netizen asked Dec 14, 2013 at 18:14 NetizenNetizen 1791 gold badge4 silver badges18 bronze badges 1
  • 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 Commented Dec 15, 2013 at 8:56
Add a comment  | 

1 Answer 1

Reset to default 3

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' );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742325969a4422754.html

相关推荐

  • custom field - 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 c

    9小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信