【发布时间】:2019-01-27 00:34:06
【问题描述】:
我在 WordPress 中有这样的页面结构:
Materials //parent
-> Plastic //children
-> Metal //children
-> ...
我想将我的文件 grid-fasads.php 中的内容与简码准确地包含在我需要的位置。
我在 product.php 中有此代码,我在其中使用简码:
$fasads = get_field('fasad_type');
foreach ($fasads as $f) {
echo do_shortcode('[grid-fasad]');
}
我在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' );
我知道问题出在( "page_id = " . $page)。我需要为“材料”页面的每个子页面包含一个 .php 文件的内容。尝试将最后一部分代码插入循环,但每次都失败。
这是来自
的片段我也试过这个循环:
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));
}
但结果它显示了重复的值。出了点问题。
请帮忙。
【问题讨论】: