如果我运行此代码,就会得到所需的结果
<?php
$terms = get_terms( \'call-type\' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
echo \'\\\'\' . $term->name . \'\\\', \';
}
}
?>
result
\'“全部”、“已完成”、“正在进行”、“新建”,
但是,如果我将相同的代码放入选项的所有选择框中,则会在结果中添加斜杠。
<select class="w-100" name="mf_term">
<option value="
<?php
$terms = get_terms( \'call-type\' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
echo \'\\\'\' . $term->name . \'\\\', \';
}
}
?>
">All</option>
result
\\\'“全部”、“已完成”、“进行中”、“新建”,
How can I remove the slashes from the option in the select box
Update Code
<main>
<section id="" class="py-3">
<div id="" class="container-fuild">
<!-- Start User Level Report -->
<div id="" class="row mx-0 my-0">
<div class="col-lg-12">
<form action="" method="POST">
<div class="table-responsive ">
<table id="" class="table table-striped table-bordered text-center">
<tbody>
<tr>
<td class="align-middle">
Select Status<br />
<?php
$terms = get_terms( \'call-type\' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
$term_option = \'\';
$sep = \'\';
foreach ( $terms as $term ) {
$term_option .= $sep."\'".$term->name."\'";
$sep = ",";
}
if(!empty($term_option))
{
?>
<select class="w-100" name="mf_term">
<option value=""></option>
<option value="<?php echo $term_option ;?>">All</option>
<?php
$mf_term_args = array(
\'taxonomy\' => \'call-type\',
);
// Get Trems as array
$term2s = get_categories( $mf_term_args );
foreach ( $term2s as $term2 ) :
?>
<option value="<?php echo $term2->name ?>"><?php echo $term2->name ?></option>
<?php endforeach; ?>
</select>
<?php
}
}
?>
<?php
$mf_term = $_POST[\'mf_term\'];
$mf_term = str_replace("\\\\\\"", "\\"", $mf_term);
$mf_term = str_replace("\\\\\'", "\'", $mf_term);
$mf_term = str_replace("\\\\\\\\", "\\\\", $mf_term);
?>
</td>
<td class="align-middle">
Select Department<br />
<!-- Script Starts here -->
<?php
$user_id = get_user_meta($user->ID);
$args = array(
\'post_type\' => \'department\',
\'posts_per_page\' => -1,
);
$query = new WP_Query($args);
?>
<select class="w-100" name="mf_department">Select Department</p>
<option value=""></option>
<option value="\'Marketing\', \'Maintenance\'">
All
</option>
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<option value="<?php the_title(); ?>">
<?php the_title(); ?>
</option>
<?php endwhile; endif; ?>
</select>
<?php
$mf_department = $_POST[\'mf_department\'];
$mf_department = str_replace("\\\\\\"", "\\"", $mf_department);
$mf_department = str_replace("\\\\\'", "\'", $mf_department);
$mf_department = str_replace("\\\\\\\\", "\\\\", $mf_department);
?>
<!-- Script Starts here -->
</td>
<td class="align-middle">
Start Date<br />
<input name="mf_start_date" type="date">
<?php
$mf_start_date = $_POST[\'mf_start_date\'];
?>
</td>
<td class="align-middle">
End Date<br />
<input name="mf_end_date" type="date">
<?php
$mf_end_date = $_POST[\'mf_end_date\'];
?>
</td>
<td class="align-middle">
By Store
</td>
<td class="align-middle">
By Area
</td>
<td class="align-middle">
By Region
</td>
<td class="align-middle">
<button class="btn btn-lg btn-primary btn-block" type="submit">Run Query</button>
</td>
</tr>
<tr class="">
<td colspan="8">
You have Selected Status: <strong class="text-danger"><?php echo $mf_term;?></strong> in the Department: <strong class="text-danger"><?php echo $mf_department;?></strong> from Starting Date: <strong class="text-danger"><?php echo $mf_start_date;?></strong> to Ending Date: <strong class="text-danger"><?php echo $mf_end_date;?></strong>.
</td>
</tr>
</tbody>
</table>
</div>
</form>
</div>
<div id="" class="col-lg-12">
<div class="table-responsive ">
<table id="data-table-1" class="table table-striped table-bordered">
<thead>
<tr>
<th>Call Ref#</th>
<th>Department</th>
<th>Type of Call</th>
<th></th>
<th>Action</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Call Ref#</th>
<th>Department</th>
<th>Col 3</th>
<th>Col 4</th>
<th>Col 5</th>
</tr>
</tfoot>
<tbody class="text-center">
<?php $query = new WP_Query(array(
\'posts_per_page\' => -1,
\'post-type\' => \'call\',
\'date_query\' => array(
array(
\'after\' => $mf_start_date,
\'before\' => $mf_end_date,
\'inclusive\' => true,
),
),
//\'terms\' => \'All, New\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'call-type\',
\'field\' => \'name\',
\'terms\' => array( $mf_term ),
),
),
\'author\' => $current_user->ID,
\'meta_query\' => array(
array(
\'key\' => \'_call_44\',
\'value\' => [ $mf_department ],
),
)
) ); ?>
<?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
<tr>
<td class="align-middle">
<?php echo $data = get_post_meta(get_the_ID(), \'_call_22\', true); ?>
</td>
<td class="align-middle">
<?php echo $data = get_post_meta(get_the_ID(), \'_call_44\', true); ?>
</td>
<td class="align-middle">
<?php echo $data = get_post_meta(get_the_ID(), \'_call_45\', true); ?>
</td>
<td class="align-middle">
</td>
<td class="align-middle">
</td>
</tr>
<?php endwhile; endif; ?>
</tbody>
</table>
</div>
</div>
</div>
<!-- End User Level Report -->
</div>
</section>
</main>
SO网友:WP Updoot
你需要做的是双倍逃跑,所以改变echo \'\\\'\' . $term->name . \'\\\', \';
到echo \'\\\\\\\'\' . $term->name . \'\\\\\\\', \';
。。。但是,您最好只使用不同的封装器:
<select class="w-100" name="mf_term">
<option value="
<?php
$terms = get_terms( \'call-type\' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
echo "\'{$term->name}\', ";
}
}
?>
">All</option>
当您回显一个字符串,而不是直接将内容附加到HTML中时,您可以使用双引号来封装输出,而无需担心过早意外退出select对象。
此外,在对标准PHP变量使用双引号时($myVariable
) 或类/对象属性($myClass->myProperty
/ $myObject->myProperty
) 您只需将其插入,PHP就会正确地评估它(我喜欢添加大括号以便更好地衡量,但这不是绝对必需的);这意味着你根本不需要逃逸字符串。
最后您提交的代码只会在下拉列表中创建一个选项,值为\'All\', \'Completed\', \'In progress\', \'New\',
. 如果您希望在选择框中列出选项列表(以便您可以选择All
, Completed
, In progress
or New
), 以下可能更有效:
<select class="w-100" name="mf_term">
<?php
$terms = get_terms( \'call-type\' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
echo "<option value=\'{$term->name}\'>{$term->name}</option>";
}
}
?>
</select>