无法在 wordpress 中将 select 框显示为 ajax 响应

Not able to display select box as ajax response in wordpress

我有两个下拉框,一个是自定义分类法,另一个是在当前选择的分类法下自定义 post。为此,我写了一个 on-change 函数。我在显示输出时遇到了一些问题 它显示这样的输出

8Associate<select class="form-control" name="upload_designation" id="upload_designation" >

<option value="">Select Designation</option><option value=""></option></select>

而不是

<select class="form-control" name="upload_designation" id="upload_designation" >

<option value="">Select Designation</option>
<option value="8">Associate</option></select>

我post正在使用我的更改功能 请帮我解决问题

function change_desgination() {
$post_val=explode("_",$_POST['id']);
$id=$post_val[0];
$taxonomy_name=$post_val[1];


$out='<select class="form-control" name="upload_designation" id="upload_designation" >
<option value="">Select Designation</option>';

    $args = array(
    'post_type' => 'designation',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'domain',
            'field' => 'slug',
            'terms' => $taxonomy_name
        )
    )
);
$the_query = new WP_Query( $args );

while ( $the_query->have_posts() ) : $the_query->the_post();
$theid=the_ID();
$thetitle=the_title();

  $out.='<option value="'.$theid.'">'.$thetitle.'</option>';

endwhile;
  $out.='</select>';
  die($out);
 } 

可能是引号有问题。任何人都请帮我找到问题所在。提前致谢。

检查了你的代码我建议你使用 foreach 循环而不是 while 如下

<?php

$out='<select class="form-control" name="upload_designation" id="upload_designation" >
<option value="">Select Designation</option>';

$array = array('one','two','three','four');

foreach ($array as $key => $value) {
  $out.='<option value="'.$key.'">'.$value.'</option>';
}
  $out.='</select>';
  die($out);
?>

用代码中显示的数组替换您的数据

问题出在

$theid=the_ID();
$thetitle=the_title();

the_ID()the_title() 将回显这些值 其中 get_the_ID();get_the_title(); 将 return 值作为字符串。 所以我用了

$theid=get_the_ID();
$thetitle=get_the_title();

参考链接

https://wordpress.stackexchange.com/questions/48523/get-title-of-post-without-using-the-title

https://developer.wordpress.org/reference/functions/get_the_id/