Created
December 18, 2012 22:47
-
-
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…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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