繁体   English   中英

如何正确关闭查询,使其不会与其他查询拦截?

[英]How to properly close a query so it doesn't intercept with other queries?

在我的模板部分,我使用了一个如下所示的函数:

function job_listings($id = null) {
$output = '';

$args = array(
    'post_type' => 'job-listings',
);

$loop = new WP_Query($args);

while($loop->have_posts()) {
    $loop->the_post();

    if($id == get_the_id()) {
        $output .= '<option selected value="'.get_the_ID().'">'.get_the_title().'</option>';
    } else {
        $output .= '<option value="'.get_the_ID().'">'.get_the_title().'</option>';
    }

}

wp_reset_postdata();

return $output;

}

...而且我一直在努力意识到问题是什么,但我现在看到,每次我尝试使用the_ID()get_the_ID()之后,ID 上方的函数都会打印来自特定函数的 ID,我没有得到,因为我认为wp_reset_postdata()正在关闭查询。

例如:

<form id="job-apply-<?php the_ID(); ?>" action="" method="post" autocomplete="off" data-url="<?php echo admin_url('admin-ajax.php'); ?>">
            <div class="form-group">
                <label for="full-name">Full Name:</label>
                <input type="text" class="form-control form-control-lg" id="full-name">
            </div>
            <div class="form-group">
                <label for="email">Email address:</label>
                <input type="email" class="form-control form-control-lg" id="email">
            </div>
            <div class="form-group">
                <label for="sel1">Select Position:</label>
                <select class="form-control form-control-lg" id="job-position">
                    <?php job_listings(get_the_ID()); ?>
                </select>
            </div>
            <div class="form-group">
                <label class="control-label">Upload Resume</label>
                <input type="file" class="filestyle" data-buttonText="Select a File" id="resume-<?php the_ID(); ?>">
            </div>
            <div class="form-group">
                <label for="comment">Additional Comments:</label>
                <textarea class="form-control" rows="5" id="comment"></textarea>
            </div>
            <button type="submit" class="btn btn-default btn-lg">Apply</button>
        </form>

form id="job-apply-<?php the_ID(); ?>"的 id 中打印当前帖子的 ID 就像它应该的那样,但是在input id="resume-<?php the_ID(); ?>"的 id 中使用函数job_listings()之后id="resume-<?php the_ID(); ?>"打印上述函数查询中的最后一个 ID。

任何帮助,将不胜感激。

在 if 语句中更改 ID。 将 get_the_id() 更改为 $loop->ID 使用 $loop 内的 ID。

if($id == $loop->ID) {
    $output .= '<option selected value="'.$loop->ID.'">'.get_the_title($loop->ID).'</option>';
} else {
    $output .= '<option value="'.$loop->ID.'">'.get_the_title($loop->ID).'</option>';
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM