2017-07-19 46 views
0

我找不到解決方案,我對WordPress模板很陌生。使用父模板中的模板樣式加載兒童內容

我想從我的活動頁面加載所有直接孩子,並將它們與模板一起加載,該模板還使用一個在acf中添加的圖像。這個想法是爲加載的內容提供超過1個靜態設計。

這是我正在使用的代碼,但the_content()只加載內容字段本身,而不是模板或acf圖像。

我試過get_post,但它不起作用。

我真的很感激,如果你能幫助我。 :)

最良好的祝願,

邁克

<?php 
 
$args = array(
 
    'post_type'  => 'page', 
 
    'posts_per_page' => -1, 
 
    'post_parent' => $post->ID, 
 
    'order'   => 'ASC', 
 
    'orderby'  => 'menu_order' 
 
); 
 

 

 
$parent = new WP_Query($args); 
 

 
if ($parent->have_posts()) : 
 
    while ($parent->have_posts()) : $parent->the_post(); 
 
\t the_content(); 
 
    endwhile; 
 

 
endif; 
 
wp_reset_query(); 
 
?>

回答

0

您所查詢的是合理的,但問題是,你不拉任何在循環的信息的。

WordPress的工作方式是,在您正在使用的單個模板文件中,您需要從此父模板中的這些頁面指定佈局的細節。

它是如何完成的,完全取決於你的子頁面的佈局,你的模板文件結構(你是否將你的模板分成更小,可重用的元素(即使用get_template_part())等等...)

從本質上講,你需要加入到這個模板是你的工作,在佈局中附加的內容,你需要

參見下面的例子爲一個粗略的想法:

<?php 
$args = array(
    'post_type'  => 'page', 
    'posts_per_page' => -1, 
    'post_parent' => $post->ID, 
    'order'   => 'ASC', 
    'orderby'  => 'menu_order' 
); 


$parent = new WP_Query($args); 
?> 

<?php if ($parent->have_posts()) : ?> 
    <?php while ($parent->have_posts()) : $parent->the_post(); ?> 

     <h3><?php the_title(); ?> 
     <div class="img-from-child"> 
      <img src="<?php get_field('img_src_from_child'); ?>" /> 
     </div> 
     <?php the_content(); ?> 

    <?php endwhile; 
<?php endif; ?> 
<?php wp_reset_query(); ?>