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条回答 默认 最新

  • douren6035 2019-02-03 14:55
    已采纳

    I've solved this problem.

    Here is a function:

    function get_pallette() {
    $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_by_title('Materials');                        
    $materials_children = get_page_children( $materials->ID, $all_wp_pages );
    
    if (isset ($materials_children)) :                             
        foreach ($materials_children as $children) {        
            $page = $children->ID;             
        }         
        $fasads = get_field('fasad_type');                                                                                      
        if (isset ($fasads)) :
            foreach ($fasads as $f) {
                $p = get_page_by_title( $f );                            
                if (!empty ($p)) :                                 
                    echo do_shortcode("[grid-fasad page=\"$p->ID\"]");                     
                else : 
                    //do something;
                endif;
            }
        else : 
            //do something;;
        endif;             
     endif; 
     //do something;
    }
    

    And here is another function for a generating shortcode:

    function get_fasads($atts, $content = null ){               
    
    $atts = shortcode_atts(
        array(
            'page' => 0  
        ), $atts, 'grid-fasad');    
    $recent = new WP_Query('page_id='. $atts['page']);
    $path = $_SERVER['DOCUMENT_ROOT'].'/wp-content/themes/theme/fasads/grid-fasads.php';        
    while ($recent->have_posts()) : $recent->the_post();
        require $path;                 
    endwhile;         
    }      
    
    add_shortcode('grid-fasad','get_fasads');
    

    A good's page fragment:

    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); global $_product; $_product = new jigoshop_product( $post->ID ); ?>
    <div id="post-<?php $id_page ?>" <?php post_class(); ?>>            
    <div class="summary">                                                                                      
        <?php  do_action('jigoshop_after_single_product_summary', $post, $_product); ?>  
        ...
        <?php get_pallette(); ?>                    
    </div>  
     <?php endwhile; ?>
    

    Maybe it's not an elegant decision, but it works. There is only 1 problem still: after execution function get_pallette() the page loose it's own ID, because I'm using "new WP_Query" with a new IDs of page's children. And if this function goes before action "jigoshop_after_single_product_summary" (as I want to), it cannot load a content of an actual good's page.

    点赞 评论

相关推荐 更多相似问题