WordPress定制帖子类型与自定义元:Foreach只循环一次

时间:2017-03-03 09:46:28

标签: php html wordpress foreach custom-post-type

我有一个名为'评论'的自定义帖子类型。其中包含自定义元数据(_field_1,_field_1_comments,_field_2,_field_2_comments等)。

我使用Insert PHP Code Snippet将PHP代码段拖到特定页面上。该片段应该在类别中找到最后三个评论并显示其自定义元。这是我正在使用的代码:

<?php
$rev = array(
    "_field_1" => "This is the first field name:",
    "_field_2" => "This is the second field name:",
    "_field_3" => "This is the third field name:",
    "_field_4" => "This is the fourth field name:",
    "_field_5" => "This is the fifth field name:"
);

$my_query = new WP_Query('post_type=reviews&cat=2');
$l = 4; ?>

<div class="feed review-feed">
    <table>
        <tr class="title">
            <td class="section">
                <h4>Fields:</h4>
            </td>
            <?php $i = 1;
            while ($my_query->have_posts() && $i < 4) : $my_query->the_post(); ?>
                <td>
                    <h4><?php the_title(); ?></h4>
                </td>
            <?php $i++; endwhile ?>
        </tr>
        <?php foreach ($rev as $field => $name) {
            $my_query->the_post();

            $comments = $field . '_comments';

            $ratings = get_post_meta(get_the_ID(), $field, true);
            $comment = get_post_meta(get_the_ID(), $comments, true); ?>

            <tr>
                <td class="section">
                    <span><?php echo $name; ?></span>
                </td>
                <td>
                    <div class="review-rating">
                        <span><?php echo $ratings; ?></span>
                    </div>
                    <div class="review-comment">
                        <?php echo $comment; ?>
                    </div>
                </td>
            </tr>
        <?php } ?>
    </table>
</div>
<?php

foreach似乎正在显示每个字段名称($ name),但只显示前两个字段($ ratings),并且只显示第一个评论($ comment)。

此外,除了第一个帖子之外,foreach没有为任何帖子获取任何HTML。我认为我的foreach肯定有问题,但我看不清楚。

0 个答案:

没有答案