I'm using the plugin homepage control in many projects and really like what it does. I can create custom sections for my static homepage and can sort them in the customizer. In my recent client project, I thought it would be a good idea to create this kind of sections according to the subpages of my homepage. -I totally failed. First I tried to use variable function names but I wasn't able to access the functions with add_action. Then I tried to use an anonymous function within the add_action call, but now I'm not able to pass my content variables into it.
Maybe my whole approach is going the wrong direction, has someone a hint or better solution for me?
Thanks in advance.
if ( ! function_exists( 'homepage_content' ) ) {
function homepage_content() {
$the_id = 5;
$item = get_post( $the_id );
$page_tree_array = get_pages( array(
'child_of' => $the_id
) );
foreach( $page_tree_array as $item ) {
$id = $item->ID;
$slug = $item->post_name;
$title = apply_filters( 'the_title', $item->post_title );
$content = apply_filters( 'the_content', $item->post_content );
$function_name = 'homepage_section_' . $slug;
add_action( 'homepage', function() {
?>
<article id="post-<?php echo $id; ?>" <?php post_class( 'homepage-content' ); ?>>
<h1><?php echo $title; ?></h1>
<?php echo $content; ?>
</article>
<?php
}, 60 );
}
}
}