admin管理员组文章数量:1410717
If I run this code I get the desired result
<?php
$terms = get_terms( 'call-type' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
echo '\'' . $term->name . '\', ';
}
}
?>
result
'All', 'Completed', 'In progress', 'New',
But if I put the same code into an option all the select box it adds slashes to the results.
<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
\'All\', \'Completed\', \'In progress\', \'New\',
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>
If I run this code I get the desired result
<?php
$terms = get_terms( 'call-type' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
echo '\'' . $term->name . '\', ';
}
}
?>
result
'All', 'Completed', 'In progress', 'New',
But if I put the same code into an option all the select box it adds slashes to the results.
<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
\'All\', \'Completed\', \'In progress\', \'New\',
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>
Share
Improve this question
edited Nov 13, 2019 at 9:50
MFWebMaster
asked Nov 13, 2019 at 6:05
MFWebMasterMFWebMaster
114 bronze badges
5
|
2 Answers
Reset to default 1What you'd need to do is double-escape, so change echo '\'' . $term->name . '\', ';
to echo '\\\'' . $term->name . '\\\', ';
...but, you'd be much better off just using different encapsulators:
<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>
As you're echo'ing a string that, not directly appending content to the HTML, you can use the double-quotes to encapsulate the output without having to worry about accidentlly exiting your select object early.
In addition, when using double-quotes with a standard PHP variable ($myVariable
) or class / object property ($myClass->myProperty
/ $myObject->myProperty
) you can just shove it in, and PHP will assess it correctly (I like to add curly braces for good measure, but it's not absolutely required); meaning you don't have to escape the string at all.
Finally; the code you've submitted will only create a single option in your dropdown, with the value of 'All', 'Completed', 'In progress', 'New',
. If you're looking to have a list of options in your select box (so you can select All
, Completed
, In progress
or New
), the following may be more effective:
<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>
Here is a clean way to achieve your need. below code return option value like :
'All', 'Completed', 'In progress', 'New'
$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="<?php echo $term_option ;?>">All</option>
</select>
<?php
}
}
}
本文标签: custom taxonomygetterms ltselectgt ltoptiongt adds slaces to the resualt
版权声明:本文标题:custom taxonomy - get_terms <select> <option> adds slaces to the resualt 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745008050a2637381.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
value=""
attribute in the HTML? Or are you looking at$_POST['mf_term']
or$_GET['mf_term']
? – Jacob Peattie Commented Nov 13, 2019 at 6:22