admin管理员组

文章数量:1122846

I created a plugin that adds content from another post, and places this content next to the original content. I can say, that this content should appear at the bottom of the original post content.

I get post_content for my additional post, and then call apply_filter('the_content', $custom) but this code does this not only for my $custom, but for original content as well! What am I doing wrong?

add_filter( 'the_content', 'custom_content' );

function custom_content( $content ) {

    $customPost = get_post( 22 );
    remove_filter( 'the_content', 'custom_content' );
    $html = $customPost->post_content;
    $html = apply_filters( 'the_content', $html );

    return $content . $html;

}

I created a plugin that adds content from another post, and places this content next to the original content. I can say, that this content should appear at the bottom of the original post content.

I get post_content for my additional post, and then call apply_filter('the_content', $custom) but this code does this not only for my $custom, but for original content as well! What am I doing wrong?

add_filter( 'the_content', 'custom_content' );

function custom_content( $content ) {

    $customPost = get_post( 22 );
    remove_filter( 'the_content', 'custom_content' );
    $html = $customPost->post_content;
    $html = apply_filters( 'the_content', $html );

    return $content . $html;

}
Share Improve this question edited May 25, 2016 at 20:59 Gabriel 2,24810 gold badges22 silver badges24 bronze badges asked May 25, 2016 at 17:49 d7p4xd7p4x 1112 bronze badges 4
  • When you say "this code does this" can you describe in more detail what happens and what you would prefer to happen? – Andy Macaulay-Brook Commented May 26, 2016 at 7:25
  • Yep :) I want to get filtered content of post, wit applied filters,hooks, etc like in normal mode - and place it after content exactly. It should look that I open Post #1, I see render result, and at the end of this post I want include Post #2 content. It works with simple blog posts, but with page builder (for example Avia) it brokes, and put extra tags.. – d7p4x Commented May 26, 2016 at 10:54
  • Looks like I should reset everything or something like this, and then render it again..huh – d7p4x Commented May 26, 2016 at 10:55
  • Ah if you're content is being messed about by a third party page builder then I wouldn't know, sorry. – Andy Macaulay-Brook Commented May 26, 2016 at 10:57
Add a comment  | 

1 Answer 1

Reset to default 0

the_content will return the whole content of the post and not particular part. If you want to access your particular part, then you can go for shortcode.

For eg, if you have content like this in your post

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's [mycustomtextpart] standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has

Then use do_shortcode function to make changes in the [mycustomtextpart] section. Refer.

本文标签: plugin developmentHow to filter content for specific content variable