【问题标题】:WordPress: Function for loading a specific page with shortcodeWordPress:使用简码加载特定页面的功能
【发布时间】: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));
}      

但结果它显示了重复的值。出了点问题。

请帮忙。

【问题讨论】:

    标签: php wordpress shortcode


    【解决方案1】:

    我已经解决了这个问题。

    这是一个函数:

    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;
    }
    

    这是另一个生成短代码的函数:

    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');
    

    一个好的页面片段:

    <?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; ?>
    

    也许这不是一个优雅的决定,但它确实有效。 仍然只有 1 个问题:在执行函数 get_pallette() 之后,页面失去了它自己的 ID,因为我正在使用带有页面子项的新 ID 的“new WP_Query”。如果此函数在操作“jigoshop_after_single_product_summary”之前(如我所愿),它无法加载实际商品页面的内容。

    【讨论】:

      猜你喜欢
      • 2019-10-23
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多