0
私はワードプレス標準に基づいてコード化したこのウィジェットを持っています。私は、バックエンドとフロントエンドの両方の目的で使用する変数を宣言しました。getElementByIdはWP変数から空を返します
public function widget($args, $instance) {
global $post;
$sid = $instance['id'];
$name = $instance['name'];
$args = array('post_type' => 'content_manager','post_id' => $sid);
$query = new WP_Query($args);
echo $args['before_widget'];
echo $args['after_widget'];
}
public function form($instance) {
global $post;
$args = array('post_type' => 'content_manager'); ....
$loop = new WP_Query($args);
$title = ! empty($instance['title']) ? $instance['title'] : esc_html__('New title', 'text_domain');
$sid = ! empty($instance['id']) ? $instance['id'] : esc_html__('New ID', 'text_domain');
$name = ! empty($instance['name']) ? $instance['name'] : esc_html__('Empty Content', 'text_domain');
私はウィジェットをロードすると、入力およびその他のオプションは、名前とIDが表示されますが、私は保存するとしない限り、JavaScriptで空を返す
<script>
function defineValue(sel)
{
var title = sel.options[sel.selectedIndex].text;
var id = sel.options[sel.selectedIndex].value;
document.getElementById("<?php echo esc_attr($this->get_field_id('title')); ?>").value = title;
document.getElementById("<?php echo esc_attr($this->get_field_name('id')); ?>").value = id;
document.getElementById("<?php echo esc_attr($this->get_field_name('name')); ?>").value = title;
}
</script>
JavaScriptで値を取得するために、変数を使用していますそれが最初に。
私は
<?php echo esc_attr($this->get_field_name('id')); ?>
それをテストしようとしましたが、それが起こるのであるなぜこれが
widget-content-loader-__i__-id
結果ですか?
注:私はこのことからDOM
<input class="widefat" name="widget-content-loader[21][title]" id="widget-content-loader-21-title" value="New title" type="text">
にこのスクリプトをチェックしたときに
まず入力および他の要素は、その変数からIDやその他の属性を持つ:中
<input
class="widefat"
name="<?php echo esc_attr($this->get_field_name('title')); ?>"
type="text"
id="<?php echo esc_attr($this->get_field_id('title')); ?>"
value="<?php echo esc_attr($title); ?>">
感謝をadvance
大丈夫、メモを追加しました。 javascriptがなくても、<?php echo esc_attr($ this-> get_field_name( 'title'));を使ってデータを取得することができます。 ?>これは入力などの要素に作用するためですが、運動しません – leojarina