Skip to content

Instantly share code, notes, and snippets.

@ericmann
Created December 18, 2012 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericmann/4332800 to your computer and use it in GitHub Desktop.
Save ericmann/4332800 to your computer and use it in GitHub Desktop.
A hacky example of something I'm actually doing in a client project. We need to include the content of sub posts/pages on a parent post/page using the `the_content` filter. We also need to pass the content of the child posts/pages through the `the_content` filter, so it's important to remove the filter immediately otherwise it's recursively appl…
<?php
add_filter( 'the_content', 'my_content_filter' );
/**
* Auto appends the content of child posts to the main post
*/
function my_content_filter( $content ) {
global $post;
if ( ! is_object( $post ) || ! is_singular( 'post' ) || 0 != $post->post_parent ) {
return $content;
}
// Remove filter so it's not added to our child posts as well
remove_filter( 'the_content', 'my_content_filter' );
// Set up child posts
$children = get_children(
array(
'post_type' => 'post',
'post_parent' => $parent->ID
)
);
if ( count( $children ) > 0 ) {
foreach( $children as $child ) {
$content .= '<br />' . apply_filters( 'the_content', $child->post_content );
}
}
return $content;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment