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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行