1

我正在查询存档模板中的自定义帖子类型。每个帖子都附加了元值。该循环确实列出了每个帖子,但每个帖子上的元值都是从第一个帖子中提取的。所以,我有一个帖子列表,所有的元值都是相同的,数据保存在第一个帖子上。数据已正确保存在数据库中。这是我的循环:

$current_time = current_time('mysql'); 
list( $today_year, $today_month, $today_day, $hour, $minute, $second ) = split( '([^0-9])', $current_time );
$current_timestamp = $today_year . $today_month . $today_day . $hour . $minute;


$args = array( 'post_type' => 'event',
'order' => 'ASC',
'orderby' => 'meta_value_num',
'posts_per_page' => 20,
'meta_key' => '_start_eventtimestamp',
'meta_query' => array(
    array(
        'key' => '_start_eventtimestamp',
        'value' => $current_timestamp,
        'compare' => '>'
    )
)
 );
// Gets the event start month from the meta field
$month = get_post_meta( $post->ID, '_start_month', true );
// Converts the month number to the month name
//$month = $wp_locale->get_month_abbrev( $wp_locale->get_month( $month ) );
// Gets the event start day
$day = get_post_meta( $post->ID, '_start_day', true );
// Gets the event start year
$year = get_post_meta( $post->ID, '_start_year', true );
$numdays = get_post_meta( $post->ID, '_tour_numdays', true );
$price = get_post_meta( $post->ID, '_tour_price', true );

query_posts( $args );

    while (have_posts() ) : the_post();
    echo '<ul>';
        echo '<li>' . $month . '/' . $day .'</li>';
        echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
        echo '<li>' . $numdays . '</li>';
        echo '<li>' . $price . '</li>';
        echo '<li><a href="#">Join this Trip</a></li>';
    echo '</ul>';
    endwhile;

我找不到解决方案。我尝试将查询从更改为WP_Queryquery_posts但仍然没有运气。

4

1 回答 1

2

这是你的逻辑:

Get Post Metas ($price, $month, etc.)

Query Post (retuned an array of posts)

Begin looping the array of posts

Echo the results, including Post Metas ($price etc) which is NOT the metas of each post

End loop

get_post_meta()您需要通过在循环内使用来获取每个帖子的元数据。

你目前正在做:

$month = get_post_meta( $post->ID, '_start_month', true );

... $post->ID不是每个帖子的 ID,只有第一个。

这是 while() 应该是什么样子的示例(我最好不要提供确切的示例,因为我可能会错过一些可能会增加混乱的东西):

while (have_posts() ) : the_post();

$month = get_post_meta( $post->ID, '_start_month', true ); // here it is
// $day = ... continue here

echo '<ul>';
    echo '<li>' . $month . '/' . $day .'</li>';
    echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
    echo '<li>' . $numdays . '</li>';
    echo '<li>' . $price . '</li>';
    echo '<li><a href="#">Join this Trip</a></li>';
echo '</ul>';
endwhile;
于 2013-02-05T03:37:27.523 回答