donglin5770
2019-01-27 00:34
浏览 22
已采纳

WordPress:用于加载具有短代码的特定页面的功能

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.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题