每次我想在 wordpress 中检索特定数据列表时,是否需要 运行 循环?

Do I need to run a loop every time I want to retrieve a particular list of data in wordpress?

更新:

我可以 运行 循环位于页面的最顶部,但是一旦我退出循环 (endwhile;) 我就无法放置我的数据数组反正。关闭循环后,我的数据无法放置在任何地方。

所以假设我会尝试:

<?php /* Template Name: memberplatform */ ?>
<?php $current = $_SERVER["REQUEST_URI"]; ?>
<?php get_header(); ?>
<?php $temp_query = $wp_query;
query_posts("cat=2&showposts=20");
while (have_posts()) : the_post();
$membername = get_post_meta( $post->ID, 'name', true );
$memberemail = get_post_meta( $post->ID, 'email', true );
$members = [];
if ( ! empty( $membername ) ) {
    $members[] = '<option value="' . $memberemail . '">' . get_post_meta($post->ID, 'name', true) .'</option>';
    echo implode($members);

}
endwhile;
?>

然后在表单中我尝试像这样回显我的数组:

<select class="drops" name="smallsan">
    <?php echo implode($members); ?>
</select>

我可以在循环中获取我的数组,所以首先,但是我不能用我的结果填充我的下拉列表。

原始问题:

我需要检索我已经在 wordpress 中提供自定义字段的人员的列表特征,例如,他们的电子邮件或他们的公司名称,以在 select 的选项标签中显示它们表单中的下拉列表。

到目前为止一切顺利,但由于我需要显示两个不同的字段(从一个人到另一个人),我不得不以这种方式 运行 两个不同的循环:

<form role="form" class="contactForm contact-form form-horizontal">
    <div class="form-group">
        <div class="controls">
            <label class="col-sm-2 control-label"> Your Name (required)</label>
            <div class="col-sm-10">
                <select class="drops" name="smallsan">
                    <?php $temp_query = $wp_query; ?>
                    <?php query_posts("cat=2&showposts=20"); ?>
                    <?php while (have_posts()) : the_post(); ?>
                        <?php 
                        $membername = get_post_meta( $post->ID, 'name', true );
                        $memberemail = get_post_meta( $post->ID, 'email', true );
                        if ( ! empty( $membername ) ) {
                            echo '<option value="' . $memberemail . '">' . get_post_meta($post->ID, 'name', true) .'</option>';
                        }
                        ?>
                        <?php

                        endwhile;
                        ?>
                    </select>
                    <div class="help-block with-errors"></div>
                </div>
            </div>
            <div class="controls">
                <label class="col-sm-2 control-label">Referring to</label>
                <div class="col-sm-10">

                    <select class="drops" name="smallsan">
                        <?php $temp_query = $wp_query; ?>
                        <?php query_posts("cat=2&showposts=20"); ?>
                        <?php while (have_posts()) : the_post(); ?>
                            <?php 
                            $membername = get_post_meta( $post->ID, 'name', true );
                            $memberemail = get_post_meta( $post->ID, 'email', true );
                            if ( ! empty( $membername ) ) {
                                echo '<option value="' . $memberemail . '">' . get_post_meta($post->ID, 'name', true) .'</option>';
                            }
                            ?>
                            ?>
                            <?php

                            endwhile;
                            ?>
                        </select>
                    </div>
                </div> 

等等。

我试图 运行 仅在最顶部的循环来提供两个部分,但它没有用,因为那样我只能检索一个值(不是所有成员他们的电子邮件):

<form role="form" class="contactForm contact-form form-horizontal">
    <?php $temp_query = $wp_query; ?>
    <?php query_posts("cat=2&showposts=20"); ?>
    <?php while (have_posts()) : the_post(); ?>
        <select class="drops" name="smallsan">
            <?php 
            $membername = get_post_meta( $post->ID, 'name', true );
            $memberemail = get_post_meta( $post->ID, 'email', true );
            if ( ! empty( $membername ) ) {
                echo '<option value="' . $memberemail . '">' . get_post_meta($post->ID, 'name', true) .'</option>';
            }
            ?>
            <?php

            endwhile;
            ?>
        </select>
        <div class="help-block with-errors"></div>
    </div>
</div>
<div class="controls">
    <label class="col-sm-2 control-label">Referring to</label>
    <div class="col-sm-10">

        <select class="drops" name="smallsan">

                <?php 
                $membername = get_post_meta( $post->ID, 'name', true );
                $memberemail = get_post_meta( $post->ID, 'email', true );
                if ( ! empty( $membername ) ) {
                    echo '<option value="' . $memberemail . '">' . get_post_meta($post->ID, 'name', true) .'</option>';
                }
                ?>
                ?>
                <?php

                endwhile;
                ?>
            </select>
        </div>
    </div>

对吗?有没有其他方法可以避免重复 (DRY) 循环并使过程窒息并使代码更清晰?

你不需要两个循环。在第一步中,您在帖子上循环一次,获取自定义字段并将它们格式化为 HTML-option 格式存储在数组中。在第二步中,您多次打印数组。

这里我提供一些示例代码:

<?php query_posts("cat=2&showposts=20"); 
$members = [];
while (have_posts())
{ 
    the_post();
    $membername = get_post_meta( $post->ID, 'name', true );
    $memberemail = get_post_meta( $post->ID, 'email', true );


    if ( ! empty( $membername ) ) {
        $members[] = '<option value="' . $memberemail . '">' . get_post_meta($post->ID, 'name', true) .'</option>';
    }
}

?>

<form role="form" class="contactForm contact-form form-horizontal">
    <div class="form-group">
        <div class="controls">
            <label class="col-sm-2 control-label"> Your Name (required)</label>
            <div class="col-sm-10">
                <select class="drops" name="smallsan">
                    <?php echo implode($members); ?>
                </select>
                <div class="help-block with-errors"></div>
                </div>
            </div>
        </div>
        <div class="controls">
            <label class="col-sm-2 control-label">Referring to</label>
            <div class="col-sm-10">

                <select class="drops" name="smallsan">
                    <?php echo implode($members); ?>
                </select>
            </div>
        </div> 
    </div>
</form>