1

我使用 Wordpress Types 插件创建了两个新的自定义帖子类型。IE。酒店和客房。Room 帖子类型是 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();?>

如何根据父酒店获取儿童房价格?

4

2 回答 2

2

在每个 Hotel 的循环内,您可以通过调用 Types 插件中包含的以下函数来访问您的 Hotel 类型的子帖子: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();?>

我希望这可以帮助你。

于 2013-08-09T16:53:49.173 回答
0

我希望我正确理解了你的问题

首先我们需要调用 Hotel Query。然后显示其图像和自定义元。然后在酒店查询中 - 显示房间帖子和元数据。

<?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; ?>
于 2013-08-09T14:28:49.253 回答