我使用Wordpress Types插件创建了两个新的Custom Post Types。即酒店和客房。Room post类型是Hotel的子级。除此之外,我还在这两种帖子类型中添加了一些自定义字段。它包括酒店地址、电话号码、电子邮件等一般详细信息。在房间帖子类型下,我添加了Room_image、Room_price和Room_description自定义字段。
我可以在单个酒店页面上显示这些自定义字段以及房间详细信息。但我被困在索引页面上,在那里我必须显示酒店详细信息的摘要以及按降序排列的房价。这是我用来显示酒店详细信息的代码
<?php
// WP_Query arguments
$args = array (
'post_type' => 'hotel',
'order' => 'DESC',
'orderby' => 'date',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<article>
<header>
<figure><img width="300" height="160" alt="Placeholder" src="<?php echo get_post_meta($query->post->ID,'wpcf-image',true);?>"><div class="fit-a"></div></figure>
<h3><a href="<?php echo get_permalink($query->post->ID ); ?>"><?php echo $query->post->post_title;?></a></h3>
<?php
// do something
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();?>
如何获取基于母酒店的儿童房价?
在每个酒店的循环中,您可以通过调用Types插件中包含的以下函数来访问酒店类型的子帖子:types_child_posts('name_of_your_child_CPT')
在你的情况下,它应该是这样的:
<?php
// WP_Query arguments
$args = array (
'post_type' => 'hotel',
'order' => 'DESC',
'orderby' => 'date',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<article>
<header>
<figure><img width="300" height="160" alt="Placeholder" src="<?php echo get_post_meta($query->post->ID,'wpcf-image',true);?>"><div class="fit-a"></div></figure>
<h3><a href="<?php echo get_permalink($query->post->ID ); ?>"><?php echo $query->post->post_title;?></a></h3>
<?php
// are there rooms for current hotel (room is the name of your child Custom Post type)
$rooms = types_child_posts('room');
foreach($rooms as $room)
{
echo '<div class="room">';
// here we display the title of a room
echo '<div class="room_name">'.$room->post_title.'</div>';
// here we display a custom field (price) of a room
echo '<div class="room_price">'.array_pop(get_post_custom_values('wpcf-room-price', $room->ID)).'</div>';
echo '</div>';
}
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();?>
我希望这能帮助你。
我希望我能正确理解你的问题
首先,我们需要调用酒店查询。然后显示其图像和自定义元。然后在酒店内查询-显示房间帖子和元。
<?php
$parent = array( 'post_type' => 'hotel', 'post_parent' => 0, 'order' => 'DESC', 'orderby' => 'date');
$hotelparent = new WP_Query($parent);
if ( $hotelparent->have_posts() ) : while ($hotelparent->have_posts()) : $hotelparent->the_post();?>
<article>
<header>
<figure><img width="300" height="160" alt="Placeholder" src="<?php echo get_post_meta($hotelparent->ID,'wpcf-image',true);?>"><div class="fit-a"></div></figure>
<h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3>
</header>
<?php $child = array('post_type' => 'hotel', 'post_parent' => $post->ID, 'order' => 'DESC', 'hierarchical'=>1);
$rooms = new WP_Query( $child );
if ( $rooms->have_posts() ) : while ($rooms->have_posts()) : $rooms->the_post(); ?>
<?php echo get_post_meta($rooms->ID,'YOUR_META',true);?>
<?php endwhile; endif; ?>
</article>
<?php endwhile; endif; ?>