I have this structure of pages in WordPress:
Materials //parent
-> Plastic //children
-> Metal //children
-> ...
I want to include content from my file grid-fasads.php
with shortcode exactly where I need to.
I have this code in a product.php where I use a shortcode:
$fasads = get_field('fasad_type');
foreach ($fasads as $f) {
echo do_shortcode('[grid-fasad]');
}
I wrote this function in functions.php:
function get_fasads($atts, $content = null ) {
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query( array( 'post_type' => 'page' ) );
// get a specific page ID
$materials = get_page( 4702 );
// filter for choosing a children pages of 'materials'
$materials_children = get_page_children( $materials->ID, $all_wp_pages );
foreach ( $materials_children as $children ) {
$page = $children->ID;
echo $page;
}
// It's ok. The pages IDs are shown.
// After that I have this:
$recent = new WP_Query( "page_id = " . $page );
while( $recent->have_posts() ) : $recent->the_post();
require( '/fasads/grid-fasads.php' );
the_content();
endwhile;
}
add_shortcode( 'grid-fasad','get_fasads' );
I understand that the problem is ( "page_id = " . $page)
. I need to include a .php file's content for each children pages of 'Materials' page. Tried to insert last part of code into a loop, but failed every time.
Here is a fragment from
I also tried this loop:
foreach ($materials_children as $children) {
$page = $children->ID;
$recent = new WP_Query("page_id=".$page);
while ($recent->have_posts()) : $recent->the_post();
require('/fasads/grid-fasads.php');
the_content();
endwhile;
print_r(array_count_values((array)$page));
}
But as a result it shows a duplicates of values. Something is wrong.
Please, help.