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.
- 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
1 Answer
Reset to default 3You 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
评论列表(0条)