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

时间:2017-06-06 05:55:37

标签: javascript jquery ajax wordpress quotes

我有两个带有自定义分类的下拉框,另一个带有当前所选分类法下的自定义帖子。为此,我写了一个改变功能。显示输出时我有一些问题 它正在显示这样的输出

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>

我在这里发布我的变更功能 请帮助我找出问题

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);
 } 

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

2 个答案:

答案 0 :(得分:1)

问题在于

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

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

$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/

答案 1 :(得分:0)

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

<?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);
?>

使用代码

中显示的数组替换您的数据