【发布时间】:2017-08-20 17:07:07
【问题描述】:
我想创建一个小部件,它可以简单地从一个类别中循环五个帖子。 用户可以从小部件选项中选择类别。
现在我成功地创建了带有硬编码值(Cat ID)的小部件和循环。
我需要并且被困在的是将这个硬编码值更改为从类别下拉列表中提取的动态选择/选项,以便用户从小部件选项中进行选择。
<?php
// Initiate roms_widget_menu ends here
class roms_widget_menu extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID
'roms_widget_menu', __('Romaisa Mege menu widget', 'roms_menu_widget_domain'), array('description' => __('Designed to display a category post in mega menu (Widgets in Menus plugin must be installed)', 'roms_menu_widget_domain'),));
}
public function widget($args, $instance) {
$title = apply_filters('widget_title', $instance['title']);
$social_menu_title = apply_filters('widget_title', $instance['title']);
// before and after widget arguments are defined by themes
echo $args['before_widget'];
// Romaisa Widget user front Content
?>
<div class="row small-up-1 medium-up-5 large-up-5">
<?php
$args = array(
'cat' => 2,
'posts_per_page' => 5, //This is the hard coded value
);
$widget_module_menu_qry = new WP_Query( $args );
?>
<?php if ( $widget_module_menu_qry->have_posts() ) : while ( $widget_module_menu_qry->have_posts() ) : $widget_module_menu_qry->the_post(); ?>
<div class="column column-block">
<div class="megamenu-item">
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(array(230, 130)); ?>
</a>
<?php else: ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><img src="<?php echo get_template_directory_uri(). '/romisa-assets/img/sample/230x130.png'; ?>" alt="">
</a>
<?php endif; ?>
<div class="megamenu-item-typo text-left">
<h5 class="text-left">
<a class="megamenu-item-typo-title trans1" href="<?php the_permalink(); ?>"><b><?php the_title(); ?></b></a>
</h5>
<p><?php echo excerpt(22); ?></p>
<h6 class="megamenu-item-date"><i class="fa fa-calendar"></i> <?php the_time('F j, Y'); ?> </h6>
</div>
</div>
</div>
<?php endwhile; endif; ?>
</div>
<?php
// /Romaisa Widget user front Content
echo $args['after_widget'];
}
// Widget Backend
public function form($instance) {
if ( isset( $instance[ 'title' ] ) ) { $title = $instance['title']; }
else { $title = __( 'Custom HTML for Menu', 'roms_menu_widget_domain' ); }
?>
<!-- Example for widget category select dropdown that need to be dynamic -->
<select>
<option>Category 1</option>
<option>Category 2</option>
</select>
<?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']) : '';
return $instance;
}
} // Class roms_widget_menu ends here
【问题讨论】: