以下は、データベースにフィールドを作成するウィジェットの完全なコードです。
require get_template_directory().'/classes/widget.php';
$widgets = new wpb_widget();
function.phpファイル内
追加コードは、我々は上記のコードに含まれていwidget.phpファイル内のコードの下に追加します。
//ウィジェット
を作成
class wpb_widget extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'wpb_widget',
// Widget name will appear in UI
__('Services Widget', 'wpb_widget_domain'),
// Widget description
array('description' => __('Sample widget based on Services Widget', 'wpb_widget_domain'),)
);
}
//ウィジェットフロントエンドを作成
//アクションが
public function widget($args, $instance) {
global $wpdb;
$title = apply_filters('widget_title', $instance['title']);
$numberofpost = apply_filters('widget_title', $instance['numberofpost']);
// before and after widget arguments are defined by themes
echo $args['before_widget'];
if (! empty($title))
echo '<div class="glossymenu"><div class="acor">';
echo $args['before_title'];
echo '<h2 class="menuitem submenuheader"><span class="glossyspan">'.$title.'</span></h2>';;
echo '<div style="text-align:right">
<a href="" class="prevprog"><img src="'.get_template_directory_uri().'/images/inner_prev.png" alt="inner_prev"></a>
<a href="" class="nextprog"><img src="'.get_template_directory_uri().'/images/inner_next.png" alt="inner_next"></a>
</div>';
echo $args['after_title'];
$query = new WP_Query(array('post_type' => 'services','posts_per_page'=>$numberofpost,'orderby'=> 'ID','order' => 'DESC'));
$totalrecord = $query->post_count;
echo '<div id="programs">';
$mm = 0;
while ($query->have_posts()) : $query->the_post();
$thumb_image_url = wp_get_attachment_image_src(get_post_thumbnail_id(), array(127, 123));
if($mm == 0)
{
echo '<div class="submenu"><ul>';
}
$expert = strip_tags(get_the_excerpt());
if (strlen($expert) > 56) {
// truncate string
$stringCut = substr($expert, 0, 56);
// make sure it ends in a word so assassinate doesn't become ass...
$expert = substr($stringCut, 0, strrpos($stringCut, ' '));
}
echo '<li>
<img width="58" src="'.$thumb_image_url[0].'" alt="'.$totalrecord.'" />
<span class="bText">'.get_the_title().'</span>
<span class="sText">'.$expert.'</span></li>';
$mm++;
if($mm == 4 or ($totalrecord == $mm))
{
echo '</ul></div>';
$mm = 0;
}
endwhile; wp_reset_query();
echo '</div>';
echo '</div></div>';
echo $args['after_widget'];
}
//ウィジェットのバックエンド
public function form($instance) {
if (isset($instance[ 'title' ])) {
$title = $instance[ 'title' ];
}
else {
$title = __('Services', 'wpb_widget_domain');
}
if (isset($instance[ 'numberofpost' ])) {
$numberofpost = $instance[ 'numberofpost' ];
}
else {
$numberofpost = __('5', 'wpb_widget_domain');
}
// Widget admin form
?>
<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>
<p>
<label for="<?php echo $this->get_field_id('numberofpost'); ?>"><?php _e('Number of posts to show:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('numberofpost'); ?>" name="<?php echo $this->get_field_name('numberofpost'); ?>" type="text" value="<?php echo esc_attr($numberofpost); ?>" />
</p>
<?php
}
// Updating widget replacing old instances with new
public function update($new_instance, $old_instance) {
$instance = array();
$instance['title'] = (! empty($new_instance['title'])) ? strip_tags($new_instance['title']) : '';
$instance['numberofpost'] = (! empty($new_instance['numberofpost'])) ? strip_tags($new_instance['numberofpost']) : '';
return $instance;
}
}
を起こるところです
//クラスwpb_widgetはここから終了します