【发布时间】:2016-12-11 22:56:54
【问题描述】:
在一个页面上,我想显示一个包含所有相关子页面的列表。在子页面上,我为图像添加了 2 个自定义字段。 “normal”表示正常状态,“hover”表示悬停状态。
这是我的代码:
<?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() ) : ?>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<?php
$image = get_field('normal');
$hover = get_field('hover');
if( !empty($image) ): ?>
<style type="text/css">
.preview-page {
background-image: url('<?php echo $image['url']; ?>');
}
.preview-page:hover {
background-image: url('<?php echo $hover['url']; ?>');
}
</style>
<?php endif; ?>
<a class="preview-page" href="<?php the_permalink() ?>">
<span><?php the_title(); ?></span>
</a><!-- end #category-name -->
<?php endwhile; ?>
<?php endif; wp_reset_query(); ?>
我的问题是,所有子页面都显示有自己的标题,但都具有相同的背景图像(最后一个子页面)。如何实现每个子页面都有正确的背景图片?我猜这是循环中的一些失败。
当我在常规 img-tag 中显示图像时,它们显示正确。
更新:这是一个测试链接,您可以在其中看到原理,但始终是最后一个子页面的 2 张图片:http://dev.communicaziun.ch/wuest/jetzt-bei-wuest/
【问题讨论】:
-
你得到的图片是post parent image吗? 'post_parent' => $post->ID,
-
我不确定你的意思。在父页面上,我正在尝试获取子页面的图像。
标签: wordpress background hover advanced-custom-fields