お世話になります。
ご相談させてください。
現在、WordPressでウィジェットの自作を行っています。
幾つかの参考サイトを確認しながら進めていますが、上手くいかないので解決を探しています。
起こっている問題
①全てのウィジェット(利用できるウィジェット)に「ウィジェットの追加」が反映されてしまう。 ②ウィジェットの表示がされない。(表示方法がわからない)
解決したいこと
①自作のウィジェットにのみ「ウィジェットの追加」を反映させたい。 ②ウィジェットが画面に反映され、index.php上で表示させたい。
前提
現在、ECサイトを作成し、タクソノミーに「野菜の種類」を設定しています。扱う野菜が増えれば、 その野菜の写真と説明文を一覧でサイドバーに表示させたいと考えています。したがって、 カスタムフィールドで画像と説明欄を作成し、タクソノミーと紐付け、お客さんの方で野菜の種類を 増やしてもらう方向です。
追加で確認したいこと
①function widget( $args, $instance )で、コードの入力ができると思ったが、コードを入力して も、画像と文章の表示までされなかった。ここでコードを書けば、それが自動的に反映されると思ったが、 そうではなかったので、function widget( $args, $instance )は具体的にどんな機能を司っている のか? ②「テキスト」の欄を追加し、そこにコードを書いてもindex.phpに表示されなかった。手順の理解が 不十分と思われる。functions.phpに以下のコードを書き、sidebar.phpに <?php dynamic_sidebar('$sidebar-1'); ?>を入れているが、この手続きの手順は誤りなのか? 追加の手続きがあるとすれば、何なのか?
functions.php
function custom_widgets_init() { register_sidebar( array( 'name' => 'ブランド追加', 'id' => 'sidebar-1', 'after_widget' => '</side>', 'before_title' => '<h2 class="brand-widget">', 'after_title' => '</h2>', )); } add_action( 'widgets_init','custom_widgets_init' ); class My_Widget extends WP_Widget { function __construct() { $widget_ops = array( 'description' => 'ウィジェットの説明文です。' ); parent::__construct( false, '僕のウィジェット', $widget_ops ); } function widget( $args, $instance ) { // ウィジェットの表示処理を記述 extract($args); //連想配列に含まれるキーを変数名、値をその変数の値として新しい変数を作成 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ); echo $before_widget; if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?> <div class="widgetContent"> <?php $args = array( 'post_type' => 'Products', 'tax_query' => array( array( 'taxonomy' => 'Products-cat', 'field' => 'slug', 'terms' => 'item1', ) ) ); ?> <?php $myposts = new WP_Query($args); ?> <?php if($myposts->have_posts()): ?> <div class="row"> <?php while($myposts -> have_posts()): $myposts->the_post();?> <div class="col-md-6"> <?php // get an image field $image = get_field('brand-image'); // each image contains a custom field called 'link' $link = get_field('link', $image['ID']); // render ?> <a href="<?php echo $link; ?>"> <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" /> </a> </div> <div class="col-md-6"> <?php the_field('brand-intro',$post->ID); ?> </div> <?php endwhile; ?> </div> <?php endif; ?> <?php wp_reset_postdata(); ?> </div> <?php echo $after_widget; } function update( $new_instance, $old_instance ) { // ウィジェットの設定の更新処理を記述。主にセキュリティを考えたサニタイズ用 $instance = $old_instance; $instance['title'] = strip_tags($new_instance['title']); //不要なNULL、HTMLタグ、PHPソースコードを排除 return $instance; } function form( $instance ) { // ウィジェットの設定用フォームを記述 $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) ); //クエリーとデフォルト値のマージ(合併) $title = strip_tags($instance['title']); //タイトルを取得 ?> <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p> <?php } } add_action('widgets_init',create_function('', 'return register_widget("My_Widget");'));
あなたの回答
tips
プレビュー