您需要输入两个参数:
function add_shortcode_imageradio() {
wpcf7_add_shortcode( 'imageradio', 'imageradio_handler', true );
}
add_action( 'wpcf7_init', 'add_shortcode_imageradio' );
function imageradio_handler( $tag ){
$tag = new WPCF7_FormTag( $tag );
$atts = array(
'type' => 'radio',
'name' => $tag->name,
'list' => $tag->name . '-options' );
$input = sprintf(
'<input %s />',
wpcf7_format_atts( $atts ) );
$datalist = '';
$datalist .= '<div class="imgradio">';
foreach ( $tag->values as $val ) {
list($radiovalue,$imagepath) = explode("!", $val
);
$datalist .= sprintf(
'<label><input type="radio" name="%s" value="%s" class="hideradio" /><img src="%s"></label>', $tag->name, $radiovalue, $imagepath
);
}
$datalist .= '</div>';
return $datalist;
}
不要忘记CSS:
input.hideradio{ /* HIDE RADIO */
visibility: hidden; /* Makes input not-clickable */
position: absolute; /* Remove input from document flow */
}
.imgradio label > input + img{ /* IMAGE STYLES */
cursor:pointer;
border:2px solid transparent;
}
.imgradio label > input:checked + img{ /* (RADIO CHECKED) IMAGE STYLES */
border:2px solid #f00;
}
并使用 contactform7 中 FORM 选项卡内声明的新短代码:
[imageradio radio-312
"Value1!http://[YourImgUrl]"
"Value2!http://[YourImgUrl]"
]
在示例中需要图像作为... https://wpquestions.com/Need_Image_as_a_Radio_Button_in_Contact_Form_7/19618#answer_16362他们错过了包含“add_action”,仅此而已
你可以检查CF7 doc https://contactform7.com/2015/01/10/adding-a-custom-form-tag/ also
希望有帮助!