php - wordpress 插件 - 根据用户输入在新选项卡或页面上打开的按钮
问题描述
我有一个选择字段
<select name="URL_tab">
<option name="yes" id="yes">yes</option>
<option name="no" id="no">no</option>
</select>
允许用户选择是或否。然后将该值上传到 wp_postmeta 表
function Modal_save_meta_box( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( $parent_id = wp_is_post_revision( $post_id ) ) {
$post_id = $parent_id;
}
$fields = [
'headline',
'sub_headline',
'button_text',
'date_live',
'date_close',
'destination_url',
'URL_tab',
'yes',
'no'
];
foreach ( $fields as $field ) {
if ( array_key_exists( $field, $_POST ) ) {
update_post_meta( $post_id, $field, sanitize_text_field( $_POST[$field] ) );
}
}
}
add_action( 'save_post', 'Modal_save_meta_box' );
如果用户选择是,则该按钮需要在新选项卡中打开
$selected_yes = get_post_meta(1408, 'yes', true);
if($selected_yes == 'yes'){
$url_target= 'target_"blank" ';
}else{
$url_target= 'target_"self" ';
}
然后将其添加到按钮
<a href="<?php echo $destination_url ?>" <?php echo $url_target ?>class="button-link"><?php echo $button_text;?></a>
我发现的问题是 $selected_yes == 'yes' 的结果即使选择了 yes 也不会返回值。任何想法将不胜感激。
解决方案
我会使用简单的 jquery 来检查选择了什么
输出的变化
<select id="myselect" name="URL_tab">
<option>Select Option</option>
<option name="yes" id="yes">yes</option>
<option name="no" id="no">no</option>
</select>
<a href="#" id="myselect-button" class="button-link">Link</a>
你的jQuery代码
jQuery(function($){
$('#myselect').change(function(){
if($(this).val() == 'yes'){
$('#myselect-button').attr('target','_blank');
}else {
$('#myselect-button').attr('target','_self');
}
});
});
推荐阅读
- android - 为什么当我将新版本更新到 google play 商店时,有些用户无法下载最新的应用程序?
- python - 应用程序与 buildpack 不兼容,django heroku
- ssh - 如果条件在 ssh 内失败
- c++ - 将表单 py2 更改为 py3 后,PyImport_Import 不能与 BOOST_PYTHON_MODULE 一起使用
- javascript - 有没有一种方法可以添加地图框悬停弹出窗口而无需废弃此代码?
- database - 如何处理firebase中多个where查询的索引?
- karate - 空手道有睡眠选项吗?我试图在特定时间段(例如:1 分钟)后使用空手道脚本从 json 文件中删除一个变量
- javascript - odoo 14 中的 Onchange 函数
- sql-server-2012 - 计算两个表条目之间的天数差异并使用 SQL Server 在另一个表中更新它
- json - Flutter Rest Api 连接 - 如何获取数据?