Ich möchte ein Widget erstellen, die einfach fünf Beiträge aus einer Kategorie. Benutzer kann die Kategorie aus den Widgetoptionen auswählen.Schleife durch Kategorien - WordPress Widget
Im Moment erstelle ich Widget und die Schleife mit einem hart codierten Wert (Cat ID) erfolgreich.
Was ich brauche, und ich stecke fest darin, diesen fest codierten Wert in dynamische Auswahl/Option zu ändern, die aus der Dropdown-Liste Kategorien auswählt, so dass der Benutzer aus den Widgetoptionen auswählt.
<?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
Das funktioniert wie ein Charme !. vielen vielen vielen dank. –