【问题标题】:Load Child Content with template styling in parent template在父模板中加载带有模板样式的子内容
【发布时间】:2017-07-19 17:01:06
【问题描述】:
我找不到解决问题的方法,而且我对 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();
the_content();
endwhile;
endif;
wp_reset_query();
?>
【问题讨论】:
标签:
php
wordpress
templates
【解决方案1】:
您的查询是正确的,但问题是您没有在循环中提取任何信息。
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(); ?>